Atchyuteswar commited on
Commit
6315267
1 Parent(s): 6888b05

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -37
app.py CHANGED
@@ -1,37 +1,66 @@
1
- import google.generativeai as genai
2
- from pathlib import path
3
- import gradio as gr
4
-
5
- generation_config = {
6
- "temprature": 0,
7
- "top_p" : 1,
8
- "top_k" : 32,
9
- "max_output_token" : 4096,
10
- }
11
-
12
- safety_settings = [
13
- {
14
- "category" : "HARM_CATEGORY_HARASSMENT",
15
- "threshold" : "BLOCK_MEDIUM_AND_ABOVE"
16
- },
17
- {
18
- "category" : "HARM_CATEGORY_HATE_SPEECH",
19
- "threshold" : "BLOCK_MEDIUM_AND_ABOVE"
20
- },
21
- {
22
- "category" : "HARM_CATEGORY_SEXUALLY_EXPLICIT",
23
- "threshold" : "BLOCK_MEDIUM_AND_ABOVE"
24
- },
25
- {
26
- "category" : "HARM_CATEGORY_DANGEROUS_CONTENT",
27
- "threshold" : "BLOCK_MEDIUM_AND_ABOVE"
28
- },
29
- ]
30
-
31
- genai.configure(api_key = "AIzaSyD6j1XrvNXktCFReBWo9Z5uB88fgCu6FB0")
32
-
33
- model = genai.GenerativeModel(model_name = "gemini-pro-vision",
34
- generation_config = generation_config,
35
- safty_settings = safety_settings)
36
-
37
- import_prompt = """ """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)