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