Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import google.generativeai as genai
|
| 3 |
+
import os
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
|
| 6 |
+
# Load API key from .env file
|
| 7 |
+
load_dotenv()
|
| 8 |
+
genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
|
| 9 |
+
|
| 10 |
+
# Initialize chat model
|
| 11 |
+
model = genai.GenerativeModel("gemini-1.5-flash")
|
| 12 |
+
|
| 13 |
+
# Streamlit UI
|
| 14 |
+
st.title("🤖 AI Chatbot (Gemini 1.5 Flash)")
|
| 15 |
+
|
| 16 |
+
# Add description
|
| 17 |
+
st.markdown("""
|
| 18 |
+
### About this Chatbot
|
| 19 |
+
This is an AI-powered chatbot built using:
|
| 20 |
+
* **Gemini 1.5 Flash** - Google's latest language model
|
| 21 |
+
* **Streamlit** - For the interactive web interface
|
| 22 |
+
* **Python** - For backend implementation
|
| 23 |
+
|
| 24 |
+
The chatbot can help you with:
|
| 25 |
+
- General questions and conversations
|
| 26 |
+
- Writing and analysis tasks
|
| 27 |
+
- Problem-solving and explanations
|
| 28 |
+
""")
|
| 29 |
+
|
| 30 |
+
st.write("Ask me anything!")
|
| 31 |
+
|
| 32 |
+
# Store chat history
|
| 33 |
+
if "messages" not in st.session_state:
|
| 34 |
+
st.session_state.messages = []
|
| 35 |
+
|
| 36 |
+
# Display previous messages
|
| 37 |
+
for msg in st.session_state.messages:
|
| 38 |
+
with st.chat_message(msg["role"]):
|
| 39 |
+
st.markdown(msg["content"])
|
| 40 |
+
|
| 41 |
+
# Get user input
|
| 42 |
+
user_input = st.chat_input("Type your message...")
|
| 43 |
+
|
| 44 |
+
if user_input:
|
| 45 |
+
# Display user message
|
| 46 |
+
st.chat_message("user").markdown(user_input)
|
| 47 |
+
|
| 48 |
+
# Prepare chat history for context
|
| 49 |
+
chat_history = [
|
| 50 |
+
{"role": "user" if m["role"] == "user" else "model", "parts": [m["content"]]}
|
| 51 |
+
for m in st.session_state.messages
|
| 52 |
+
]
|
| 53 |
+
|
| 54 |
+
# Call Gemini API
|
| 55 |
+
response = model.generate_content(
|
| 56 |
+
contents=[{"role": "user", "parts": [user_input]}],
|
| 57 |
+
generation_config={"temperature": 0.7},
|
| 58 |
+
safety_settings=[]
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
bot_reply = response.text
|
| 62 |
+
|
| 63 |
+
# Display bot response
|
| 64 |
+
st.chat_message("assistant").markdown(bot_reply)
|
| 65 |
+
|
| 66 |
+
# Save conversation
|
| 67 |
+
st.session_state.messages.append({"role": "user", "content": user_input})
|
| 68 |
+
st.session_state.messages.append({"role": "assistant", "content": bot_reply})
|
| 69 |
+
|
| 70 |
+
# Keep only last 3 message exchanges (6 messages total)
|
| 71 |
+
if len(st.session_state.messages) > 6:
|
| 72 |
+
st.session_state.messages = st.session_state.messages[-6:]
|