-
-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathgemini.py
More file actions
59 lines (51 loc) · 1.97 KB
/
Copy pathgemini.py
File metadata and controls
59 lines (51 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import asyncio
import json
import requests
from base64 import b64decode as m
from pyrogram import Client
from pyrogram.types import Message
from . import on_message, hellbot, HelpMenu
# Define the Gemini API function
def gemini(args: str) -> str:
url = m(
"aHR0cHM6Ly9nZW5lcmF0aXZlbGFuZ3VhZ2UuZ29vZ2xlYXBpcy5jb20vdjFiZXRhL21vZGVscy9nZW1pbmktcHJvOmdlbmVyYXRlQ29udGVudD9rZXk9QUl6YVN5QlFhb1VGLUtXalBWXzRBQnRTTjBEUTBSUGtOZUNoNHRN"
).decode("utf-8")
headers = {"Content-Type": "application/json"}
payload = {"contents": [{"parts": [{"text": args}]}]}
response = requests.post(url, headers=headers, data=json.dumps(payload))
if response.status_code == 200:
generated_text = response.json()["candidates"][0]["content"]["parts"][0]["text"]
return generated_text
else:
return "Failed to generate text using Gemini API."
@on_message("gemi", allow_stan=True)
async def gemini_handler(client: Client, message: Message):
user_input = message.text.split(None, 1)
if len(user_input) < 2:
hell = await hellbot.edit(
message,
"Please provide input text to generate a response."
)
await asyncio.sleep(10)
await msg.delete()
return
user_input = user_input[1]
hell = await hellbot.edit(message, "Generating response...")
try:
generated_text = gemini(user_input)
except Exception as e:
hell = await hellbot.edit(message, f"Error generating response: {e}")
await asyncio.sleep(10)
await msg.delete()
return
if "Failed to generate text" in generated_text:
hell = await hellbot.edit(message, generated_text)
await asyncio.sleep(10)
await hell.delete()
return
output = f"➜ Input: {user_input}\n\n➜ Response:\n{generated_text}"
msg = await hellbot.edit(message, f"{output}")
await hell.delete()
HelpMenu("gemi").add(
"gemi", "<text>", "Generates text using the Gemini language model!"
).done()