-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathmain.py
126 lines (114 loc) · 5.42 KB
/
main.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import google.generativeai as genai
from flask import Flask,request,jsonify
import requests
import os
import fitz
wa_token=os.environ.get("WA_TOKEN")
genai.configure(api_key=os.environ.get("GEN_API"))
phone_id=os.environ.get("PHONE_ID")
phone=os.environ.get("PHONE_NUMBER")
name="Your name or nickname" #The bot will consider this person as its owner or creator
bot_name="Give a name to your bot" #This will be the name of your bot, eg: "Hello I am Astro Bot"
model_name="gemini-1.5-flash-latest" #Switch to "gemini-1.0-pro" or any free model, if "gemini-1.5-flash" becomes paid in future.
app=Flask(__name__)
generation_config = {
"temperature": 1,
"top_p": 0.95,
"top_k": 0,
"max_output_tokens": 8192,
}
safety_settings = [
{"category": "HARM_CATEGORY_HARASSMENT","threshold": "BLOCK_MEDIUM_AND_ABOVE"},
{"category": "HARM_CATEGORY_HATE_SPEECH","threshold": "BLOCK_MEDIUM_AND_ABOVE"},
{"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT","threshold": "BLOCK_MEDIUM_AND_ABOVE"},
{"category": "HARM_CATEGORY_DANGEROUS_CONTENT","threshold": "BLOCK_MEDIUM_AND_ABOVE"},
]
model = genai.GenerativeModel(model_name=model_name,
generation_config=generation_config,
safety_settings=safety_settings)
convo = model.start_chat(history=[
])
convo.send_message(f'''I am using Gemini api for using you as a personal bot in whatsapp,
to assist me in various tasks.
So from now you are "{bot_name}" created by {name} ( Yeah it's me, my name is {name}).
And don't give any response to this prompt.
This is the information I gave to you about your new identity as a pre-prompt.
This message always gets executed when i run this bot script.
So reply to only the prompts after this. Remeber your new identity is {bot_name}.''')
def send(answer):
url=f"https://graph.facebook.com/v18.0/{phone_id}/messages"
headers={
'Authorization': f'Bearer {wa_token}',
'Content-Type': 'application/json'
}
data={
"messaging_product": "whatsapp",
"to": f"{phone}",
"type": "text",
"text":{"body": f"{answer}"},
}
response=requests.post(url, headers=headers,json=data)
return response
def remove(*file_paths):
for file in file_paths:
if os.path.exists(file):
os.remove(file)
else:pass
@app.route("/",methods=["GET","POST"])
def index():
return "Bot"
@app.route("/webhook", methods=["GET", "POST"])
def webhook():
if request.method == "GET":
mode = request.args.get("hub.mode")
token = request.args.get("hub.verify_token")
challenge = request.args.get("hub.challenge")
if mode == "subscribe" and token == "BOT":
return challenge, 200
else:
return "Failed", 403
elif request.method == "POST":
try:
data = request.get_json()["entry"][0]["changes"][0]["value"]["messages"][0]
if data["type"] == "text":
prompt = data["text"]["body"]
convo.send_message(prompt)
send(convo.last.text)
else:
media_url_endpoint = f'https://graph.facebook.com/v18.0/{data[data["type"]]["id"]}/'
headers = {'Authorization': f'Bearer {wa_token}'}
media_response = requests.get(media_url_endpoint, headers=headers)
media_url = media_response.json()["url"]
media_download_response = requests.get(media_url, headers=headers)
if data["type"] == "audio":
filename = "/tmp/temp_audio.mp3"
elif data["type"] == "image":
filename = "/tmp/temp_image.jpg"
elif data["type"] == "document":
doc=fitz.open(stream=media_download_response.content,filetype="pdf")
for _,page in enumerate(doc):
destination="/tmp/temp_image.jpg"
pix = page.get_pixmap()
pix.save(destination)
file = genai.upload_file(path=destination,display_name="tempfile")
response = model.generate_content(["What is this",file])
answer=response._result.candidates[0].content.parts[0].text
convo.send_message(f"This message is created by an llm model based on the image prompt of user, reply to the user based on this: {answer}")
send(convo.last.text)
remove(destination)
else:send("This format is not Supported by the bot ☹")
with open(filename, "wb") as temp_media:
temp_media.write(media_download_response.content)
file = genai.upload_file(path=filename,display_name="tempfile")
response = model.generate_content(["What is this",file])
answer=response._result.candidates[0].content.parts[0].text
remove("/tmp/temp_image.jpg","/tmp/temp_audio.mp3")
convo.send_message(f"This is an voice/image message from user transcribed by an llm model, reply to the user based on the transcription: {answer}")
send(convo.last.text)
files=genai.list_files()
for file in files:
file.delete()
except :pass
return jsonify({"status": "ok"}), 200
if __name__ == "__main__":
app.run(debug=True, port=8000)