Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,67 +1,78 @@
|
|
1 |
-
import os
|
2 |
-
import google.generativeai as genai
|
3 |
import gradio as gr
|
|
|
|
|
4 |
|
5 |
-
# Configure the API key
|
6 |
-
genai.configure(api_key=os.environ["
|
7 |
-
|
8 |
-
# Create the model
|
9 |
-
generation_config = {
|
10 |
-
"temperature": 1,
|
11 |
-
"top_p": 0.95,
|
12 |
-
"top_k": 64,
|
13 |
-
"max_output_tokens": 8192,
|
14 |
-
"response_mime_type": "text/plain",
|
15 |
-
}
|
16 |
|
17 |
-
|
18 |
-
|
19 |
-
generation_config=generation_config,
|
20 |
-
# safety_settings can be adjusted
|
21 |
-
# See https://ai.google.dev/gemini-api/docs/safety-settings
|
22 |
-
)
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
history.append({"role": "user", "content": user_input})
|
28 |
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
# Assuming the message content is a string
|
33 |
-
content = genai.protos.Content(text=entry["content"], role=entry["role"])
|
34 |
-
formatted_history.append(content)
|
35 |
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
chatbot = gr.Chatbot()
|
53 |
-
|
54 |
with gr.Row():
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
#
|
64 |
-
|
65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
|
67 |
-
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import google.generativeai as genai
|
3 |
+
import os
|
4 |
|
5 |
+
# Configure the Gemini API with your API key
|
6 |
+
genai.configure(api_key=os.environ["API_KEY"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
+
# Initialize the Gemini Generative Model
|
9 |
+
model = genai.GenerativeModel("gemini-1.5-flash")
|
|
|
|
|
|
|
|
|
10 |
|
11 |
+
def chat_with_gemini(user_input, history):
|
12 |
+
"""
|
13 |
+
Generates a response from the Gemini API based on user input and conversation history.
|
|
|
14 |
|
15 |
+
Args:
|
16 |
+
user_input (str): The latest message from the user.
|
17 |
+
history (list): The conversation history as a list of tuples.
|
|
|
|
|
|
|
18 |
|
19 |
+
Returns:
|
20 |
+
tuple: The chatbot's reply and the updated history.
|
21 |
+
"""
|
22 |
+
try:
|
23 |
+
# Send the latest message to the Gemini API
|
24 |
+
response = model.generate_content(
|
25 |
+
user_input,
|
26 |
+
generation_config=genai.GenerationConfig(
|
27 |
+
max_output_tokens=150,
|
28 |
+
temperature=0.7
|
29 |
+
)
|
30 |
+
)
|
31 |
+
|
32 |
+
chatbot_reply = response.text.strip()
|
33 |
+
|
34 |
+
# Append the user input and chatbot reply to the history
|
35 |
+
history.append((user_input, chatbot_reply))
|
36 |
+
|
37 |
+
return history, history
|
38 |
+
except Exception as e:
|
39 |
+
error_message = f"An error occurred: {e}"
|
40 |
+
history.append((user_input, error_message))
|
41 |
+
return history, history
|
42 |
|
43 |
+
with gr.Blocks() as iface:
|
44 |
+
gr.Markdown("# 🗣️ Google Gemini Chatbot")
|
45 |
+
|
46 |
chatbot = gr.Chatbot()
|
47 |
+
|
48 |
with gr.Row():
|
49 |
+
with gr.Column(scale=0.85):
|
50 |
+
user_input = gr.Textbox(
|
51 |
+
placeholder="Type your message here...",
|
52 |
+
show_label=False
|
53 |
+
)
|
54 |
+
with gr.Column(scale=0.15):
|
55 |
+
send_button = gr.Button("Send")
|
56 |
+
|
57 |
+
# State to hold the conversation history
|
58 |
+
history = gr.State([])
|
59 |
+
|
60 |
+
def respond(message, history_state):
|
61 |
+
"""
|
62 |
+
Handles the user message, generates a response, and updates the conversation history.
|
63 |
+
|
64 |
+
Args:
|
65 |
+
message (str): The user's message.
|
66 |
+
history_state (list): The current conversation history.
|
67 |
+
|
68 |
+
Returns:
|
69 |
+
tuple: Updated conversation history for display.
|
70 |
+
"""
|
71 |
+
updated_history, new_history = chat_with_gemini(message, history_state)
|
72 |
+
return updated_history, new_history
|
73 |
+
|
74 |
+
send_button.click(respond, inputs=[user_input, history], outputs=[chatbot, history])
|
75 |
+
user_input.submit(respond, inputs=[user_input, history], outputs=[chatbot, history])
|
76 |
|
77 |
+
if __name__ == "__main__":
|
78 |
+
iface.launch()
|