Commit
•
6315267
1
Parent(s):
6888b05
Update app.py
Browse files
app.py
CHANGED
@@ -1,37 +1,66 @@
|
|
1 |
-
import
|
2 |
-
|
3 |
-
import
|
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 |
-
safty_settings = safety_settings)
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import google.generativeai as genai
|
3 |
+
import time
|
4 |
+
import os
|
5 |
+
|
6 |
+
GOOGLE_API_KEY=os.getenv('AIzaSyCGMebr9R_p_hF2jbfXj0GlUJRtAI7sc3M')
|
7 |
+
genai.configure(api_key=GOOGLE_API_KEY)
|
8 |
+
|
9 |
+
# model = genai.GenerativeModel('gemini-pro')
|
10 |
+
# model = genai.GenerativeModel('gemini-1.5-flash-latest')
|
11 |
+
# chat = model.start_chat(history=[])
|
12 |
+
|
13 |
+
safety_settings = [
|
14 |
+
{
|
15 |
+
"category" : "HARM_CATEGORY_HARASSMENT",
|
16 |
+
"threshold" : "BLOCK_MEDIUM_AND_ABOVE"
|
17 |
+
},
|
18 |
+
{
|
19 |
+
"category" : "HARM_CATEGORY_HATE_SPEECH",
|
20 |
+
"threshold" : "BLOCK_MEDIUM_AND_ABOVE"
|
21 |
+
},
|
22 |
+
{
|
23 |
+
"category" : "HARM_CATEGORY_SEXUALLY_EXPLICIT",
|
24 |
+
"threshold" : "BLOCK_MEDIUM_AND_ABOVE"
|
25 |
+
},
|
26 |
+
{
|
27 |
+
"category" : "HARM_CATEGORY_DANGEROUS_CONTENT",
|
28 |
+
"threshold" : "BLOCK_MEDIUM_AND_ABOVE"
|
29 |
+
},
|
30 |
+
]
|
31 |
+
|
32 |
+
genai.configure(api_key = "AIzaSyD6j1XrvNXktCFReBWo9Z5uB88fgCu6FB0")
|
33 |
+
|
34 |
+
model = genai.GenerativeModel(model_name = "gemini-pro-vision",
|
35 |
+
safty_settings = safety_settings)
|
36 |
+
|
37 |
+
|
38 |
+
# Transform Gradio history to Gemini format
|
39 |
+
def transform_history(history,system_prompt):
|
40 |
+
new_history = []
|
41 |
+
new_history.append({"parts": [{"text": system_prompt}], "role": "user"})
|
42 |
+
for chat in history:
|
43 |
+
new_history.append({"parts": [{"text": chat[0]}], "role": "user"})
|
44 |
+
new_history.append({"parts": [{"text": chat[1]}], "role": "model"})
|
45 |
+
return new_history
|
46 |
+
|
47 |
+
def response(message, history):
|
48 |
+
global chat
|
49 |
+
# The history will be the same as in Gradio, the 'Undo' and 'Clear' buttons will work correctly.
|
50 |
+
system_prompt = f"""
|
51 |
+
Respond in style of Elon Musk always, in not more than 40 words.
|
52 |
+
Your response should be funny and interesting just like Elon Musk.
|
53 |
+
"""
|
54 |
+
chat.history = transform_history(history,system_prompt)
|
55 |
+
response = chat.send_message(message)
|
56 |
+
response.resolve()
|
57 |
+
|
58 |
+
# Each character of the answer is displayed
|
59 |
+
for i in range(len(response.text)):
|
60 |
+
time.sleep(0.005)
|
61 |
+
yield response.text[: i+20]
|
62 |
+
|
63 |
+
gr.ChatInterface(response,
|
64 |
+
title='Chat with Elon Musk (AI)',
|
65 |
+
textbox=gr.Textbox(placeholder="Ask Elon"),
|
66 |
+
retry_btn=None).launch(debug=True)
|