Ufoptg commited on
Commit
e54fb11
·
verified ·
1 Parent(s): 47ad446

Create gemini.py

Browse files
Files changed (1) hide show
  1. Hellbot/plugins/user/gemini.py +55 -0
Hellbot/plugins/user/gemini.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import asyncio
2
+ import json
3
+ import requests
4
+ from pyrogram import Client
5
+ from pyrogram.types import Message
6
+ from . import on_message, hellbot, HelpMenu
7
+
8
+ # Define the Gemini API function
9
+ def gemini(args: str) -> str:
10
+ url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=AIzaSyBQaoUF-KWjPV_4ABtSN0DQ0RPkNeCh4tM"
11
+ headers = {"Content-Type": "application/json"}
12
+ payload = {"contents": [{"parts": [{"text": args}]}]}
13
+
14
+ response = requests.post(url, headers=headers, data=json.dumps(payload))
15
+ if response.status_code == 200:
16
+ generated_text = response.json()["candidates"][0]["content"]["parts"][0]["text"]
17
+ return generated_text
18
+ else:
19
+ return "Failed to generate text using Gemini API."
20
+
21
+ @on_message("gemi", allow_stan=True)
22
+ async def gemini_handler(client: Client, message: Message):
23
+ user_input = message.text.split(None, 1)
24
+ if len(user_input) < 2:
25
+ msg = await hellbot.edit(
26
+ message,
27
+ "Please provide input text to generate a response."
28
+ )
29
+ await asyncio.sleep(10)
30
+ await msg.delete()
31
+ return
32
+ user_input = user_input[1]
33
+
34
+ msg = await hellbot.edit(message, "Generating response...")
35
+
36
+ try:
37
+ generated_text = gemini(user_input)
38
+ except Exception as e:
39
+ msg = await hellbot.edit(message, f"Error generating response: {e}")
40
+ await asyncio.sleep(10)
41
+ await msg.delete()
42
+ return
43
+
44
+ if "Failed to generate text" in generated_text:
45
+ msg = await hellbot.edit(message, generated_text)
46
+ await asyncio.sleep(10)
47
+ await msg.delete()
48
+ return
49
+
50
+ output = f"➜ Input: {user_input}\n\n➜ Response:\n{generated_text}"
51
+ msg = await hellbot.edit(message, f"{output}")
52
+
53
+ HelpMenu("gemi").add(
54
+ "gemi", "<text>", "Generates text using the Gemini language model!"
55
+ ).done()