Update app.py
Browse files
app.py
CHANGED
@@ -1,63 +1,53 @@
|
|
|
|
1 |
import gradio as gr
|
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 |
-
gr.Slider(
|
52 |
-
minimum=0.1,
|
53 |
-
maximum=1.0,
|
54 |
-
value=0.95,
|
55 |
-
step=0.05,
|
56 |
-
label="Top-p (nucleus sampling)",
|
57 |
-
),
|
58 |
-
],
|
59 |
-
)
|
60 |
-
|
61 |
-
|
62 |
if __name__ == "__main__":
|
63 |
demo.launch()
|
|
|
1 |
+
import google.generativeai as genai
|
2 |
import gradio as gr
|
3 |
+
import os # Import the os module for environment variables
|
4 |
+
|
5 |
+
# Get the API Key from Hugging Face Secrets
|
6 |
+
api_key = os.getenv("GOOGLE_API_KEY")
|
7 |
+
genai.configure(api_key=api_key)
|
8 |
+
|
9 |
+
# Initialize model and chat (outside the function)
|
10 |
+
model = genai.GenerativeModel("gemini-pro")
|
11 |
+
chat = model.start_chat(history=[])
|
12 |
+
|
13 |
+
def get_gemini_response(question):
|
14 |
+
response = chat.send_message(question, stream=True)
|
15 |
+
full_response = ""
|
16 |
+
for chunk in response:
|
17 |
+
full_response += chunk.text
|
18 |
+
return full_response
|
19 |
+
|
20 |
+
# Interface using Gradio
|
21 |
+
theme = gr.themes.Soft()
|
22 |
+
with gr.Blocks(theme=theme, css=".gradio-container {background-color: #f0f5f9}") as demo:
|
23 |
+
gr.Markdown(
|
24 |
+
"""
|
25 |
+
<div style="text-align: center;">
|
26 |
+
<h1 style="color: #007bff;">Debyez Chatbot</h1>
|
27 |
+
<p style="color: #6c757d;">Ask me anything! 💬</p>
|
28 |
+
</div>
|
29 |
+
"""
|
30 |
+
)
|
31 |
+
|
32 |
+
chatbot = gr.Chatbot(elem_classes="colorful-chatbot")
|
33 |
+
msg = gr.Textbox(label="Your Message", placeholder="Type your message here...")
|
34 |
+
|
35 |
+
# Colorful Clear Button
|
36 |
+
clear = gr.ClearButton(
|
37 |
+
[msg, chatbot],
|
38 |
+
value="Clear Conversation",
|
39 |
+
variant="primary",
|
40 |
+
)
|
41 |
+
|
42 |
+
# Response Function (unchanged)
|
43 |
+
def respond(message, chat_history):
|
44 |
+
bot_message = get_gemini_response(message)
|
45 |
+
chat_history.append((message, bot_message))
|
46 |
+
return "", chat_history
|
47 |
+
|
48 |
+
# Submit Message & Update Chat
|
49 |
+
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
50 |
+
|
51 |
+
# Launch the Chat Interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
if __name__ == "__main__":
|
53 |
demo.launch()
|