Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- app.py +69 -52
- chatbot.py +20 -0
- requirements.txt +4 -1
app.py
CHANGED
|
@@ -1,64 +1,81 @@
|
|
| 1 |
-
import
|
| 2 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
system_message,
|
| 14 |
-
max_tokens,
|
| 15 |
-
temperature,
|
| 16 |
-
top_p,
|
| 17 |
-
):
|
| 18 |
-
messages = [{"role": "system", "content": system_message}]
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
messages.append({"role": "user", "content": val[0]})
|
| 23 |
-
if val[1]:
|
| 24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 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 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 52 |
-
gr.Slider(
|
| 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 |
-
|
| 64 |
-
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from chatbot import (
|
| 3 |
+
get_googlegenai_client,
|
| 4 |
+
get_default_model,
|
| 5 |
+
load_chat_history,
|
| 6 |
+
save_chat_history
|
| 7 |
+
)
|
| 8 |
|
| 9 |
+
# Centered Title
|
| 10 |
+
st.markdown("""
|
| 11 |
+
<h1 style='text-align: center;'>Welcome, ChatGPT Clone</h1>
|
| 12 |
+
""", unsafe_allow_html=True)
|
| 13 |
|
| 14 |
+
client = get_googlegenai_client()
|
| 15 |
|
| 16 |
+
# Initialize session state
|
| 17 |
+
if "googlegenai_model" not in st.session_state:
|
| 18 |
+
st.session_state["googlegenai_model"] = get_default_model()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
if "messages" not in st.session_state:
|
| 21 |
+
st.session_state.messages = load_chat_history()
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
# Sidebar: Clear history
|
| 24 |
+
with st.sidebar:
|
| 25 |
+
if st.button("Delete Chat History"):
|
| 26 |
+
st.session_state.messages = []
|
| 27 |
+
save_chat_history([])
|
| 28 |
|
| 29 |
+
# Show chat history
|
| 30 |
+
for msg in st.session_state.messages:
|
| 31 |
+
role = msg["role"]
|
| 32 |
+
content = msg["content"]
|
| 33 |
|
| 34 |
+
if role == "user":
|
| 35 |
+
st.markdown(f"""
|
| 36 |
+
<div style='text-align: right; background-color: #f0f0f5; padding: 10px 15px; border-radius: 20px; margin: 10px 0; display: inline-block; max-width: 80%; float: right; clear: both;'>
|
| 37 |
+
{content}
|
| 38 |
+
</div>
|
| 39 |
+
""", unsafe_allow_html=True)
|
| 40 |
+
else:
|
| 41 |
+
st.markdown(f"""
|
| 42 |
+
<div style='text-align: left; margin: 10px 0; max-width: 80%; float: left; clear: both;'>
|
| 43 |
+
{content}
|
| 44 |
+
</div>
|
| 45 |
+
""", unsafe_allow_html=True)
|
| 46 |
+
st.markdown("<div style='clear: both'></div>", unsafe_allow_html=True)
|
| 47 |
|
| 48 |
+
# Input field
|
| 49 |
+
if prompt := st.chat_input("Ask anything"):
|
| 50 |
+
# Append user message
|
| 51 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 52 |
+
st.markdown(f"""
|
| 53 |
+
<div style='text-align: right; background-color: #f0f0f5; padding: 10px 15px; border-radius: 20px; margin: 10px 0; display: inline-block; max-width: 80%; float: right; clear: both;'>
|
| 54 |
+
{prompt}
|
| 55 |
+
</div>
|
| 56 |
+
""", unsafe_allow_html=True)
|
| 57 |
|
| 58 |
+
# Stream assistant response
|
| 59 |
+
full_response = ""
|
| 60 |
+
response_container = st.empty()
|
| 61 |
+
response = client.stream(st.session_state.messages)
|
| 62 |
|
| 63 |
+
for chunk in response:
|
| 64 |
+
full_response += chunk.content or ""
|
| 65 |
+
response_container.markdown(f"""
|
| 66 |
+
<div style='text-align: left; margin: 10px 0; max-width: 80%; float: left; clear: both;'>
|
| 67 |
+
{full_response + "▌"}
|
| 68 |
+
</div>
|
| 69 |
+
""", unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
+
# Final response (cleaned up)
|
| 72 |
+
response_container.markdown(f"""
|
| 73 |
+
<div style='text-align: left; margin: 10px 0; max-width: 80%; float: left; clear: both;'>
|
| 74 |
+
{full_response}
|
| 75 |
+
</div>
|
| 76 |
+
""", unsafe_allow_html=True)
|
| 77 |
+
st.markdown("<div style='clear: both'></div>", unsafe_allow_html=True)
|
| 78 |
|
| 79 |
+
# Save assistant response
|
| 80 |
+
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
| 81 |
+
save_chat_history(st.session_state.messages)
|
chatbot.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import shelve
|
| 3 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 4 |
+
|
| 5 |
+
def get_default_model():
|
| 6 |
+
return "gemini-2.5-flash"
|
| 7 |
+
|
| 8 |
+
def get_googlegenai_client():
|
| 9 |
+
return ChatGoogleGenerativeAI(model=get_default_model(), streaming=True)
|
| 10 |
+
|
| 11 |
+
def get_storage_path():
|
| 12 |
+
return os.path.join("/tmp", "chat_history")
|
| 13 |
+
|
| 14 |
+
def load_chat_history():
|
| 15 |
+
with shelve.open(get_storage_path()) as db:
|
| 16 |
+
return db.get("messages", [])
|
| 17 |
+
|
| 18 |
+
def save_chat_history(messages):
|
| 19 |
+
with shelve.open(get_storage_path()) as db:
|
| 20 |
+
db["messages"] = messages
|
requirements.txt
CHANGED
|
@@ -1 +1,4 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit>=1.32.0
|
| 2 |
+
langchain>=0.1.16
|
| 3 |
+
langchain-google-genai>=1.0.0
|
| 4 |
+
google-generativeai>=0.5.4
|