Update app.py
Browse files
app.py
CHANGED
@@ -1,292 +1,275 @@
|
|
1 |
-
import os
|
2 |
-
import uuid
|
3 |
-
|
4 |
-
import
|
5 |
-
|
6 |
-
import
|
7 |
-
from
|
8 |
-
|
9 |
-
|
10 |
-
from
|
11 |
-
from
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
from
|
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 |
-
def
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
workflow.
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
background-color:
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
.
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
current_thread_id.value
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
)
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
inputs=[
|
263 |
-
outputs=[chatbot_ui]
|
264 |
-
)
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
)
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
clear.click(
|
277 |
-
clear_current,
|
278 |
-
inputs=[current_thread_id],
|
279 |
-
outputs=[chatbot_ui]
|
280 |
-
)
|
281 |
-
|
282 |
-
return demo
|
283 |
-
|
284 |
-
|
285 |
-
|
286 |
-
# === Run App ===
|
287 |
-
if __name__ == "__main__":
|
288 |
-
try:
|
289 |
-
demo = launch_interface()
|
290 |
-
demo.launch(share=True)
|
291 |
-
except Exception as e:
|
292 |
-
logger.critical(f"App failed: {e}")
|
|
|
1 |
+
import os
|
2 |
+
import uuid
|
3 |
+
|
4 |
+
import google.generativeai as genai
|
5 |
+
|
6 |
+
from langgraph.graph import START, MessagesState, StateGraph
|
7 |
+
from langgraph.checkpoint.memory import MemorySaver
|
8 |
+
|
9 |
+
from langchain_core.messages import HumanMessage, AIMessage
|
10 |
+
from langchain_core.prompts.chat import (ChatPromptTemplate, SystemMessagePromptTemplate, MessagesPlaceholder, HumanMessagePromptTemplate,)
|
11 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
12 |
+
|
13 |
+
import gradio as gr
|
14 |
+
|
15 |
+
import time
|
16 |
+
import json
|
17 |
+
|
18 |
+
import logging
|
19 |
+
from dotenv import load_dotenv
|
20 |
+
|
21 |
+
|
22 |
+
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
|
23 |
+
logger = logging.getLogger(__name__)
|
24 |
+
load_dotenv()
|
25 |
+
|
26 |
+
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
27 |
+
if not GEMINI_API_KEY:
|
28 |
+
raise ValueError("Missing GEMINI_API_KEY")
|
29 |
+
genai.configure(api_key=GEMINI_API_KEY)
|
30 |
+
|
31 |
+
HISTORY_FILE = "chat_history.json"
|
32 |
+
|
33 |
+
|
34 |
+
def load_all_sessions():
|
35 |
+
if os.path.exists(HISTORY_FILE):
|
36 |
+
with open(HISTORY_FILE, "r", encoding="utf-8") as f:
|
37 |
+
return json.load(f)
|
38 |
+
return {}
|
39 |
+
|
40 |
+
def save_all_sessions(sessions):
|
41 |
+
with open(HISTORY_FILE, "w", encoding="utf-8") as f:
|
42 |
+
json.dump(sessions, f, indent=2)
|
43 |
+
|
44 |
+
class GeminiChatbot:
|
45 |
+
def __init__(self):
|
46 |
+
self.setup_model()
|
47 |
+
|
48 |
+
def setup_model(self):
|
49 |
+
system_template = """
|
50 |
+
You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe.
|
51 |
+
Your answers should be informative, engaging, and accurate. If a question doesn't make any sense, or isn't factually coherent, explain why instead of answering something not correct.
|
52 |
+
If you don't know the answer to a question, please don't share false information.
|
53 |
+
"""
|
54 |
+
|
55 |
+
self.prompt = ChatPromptTemplate.from_messages([
|
56 |
+
SystemMessagePromptTemplate.from_template(system_template),
|
57 |
+
MessagesPlaceholder(variable_name="chat_history"),
|
58 |
+
HumanMessagePromptTemplate.from_template("{input}")
|
59 |
+
])
|
60 |
+
|
61 |
+
self.model = ChatGoogleGenerativeAI(
|
62 |
+
model="gemini-2.0-flash",
|
63 |
+
temperature=0.7,
|
64 |
+
top_p=0.95,
|
65 |
+
google_api_key=GEMINI_API_KEY,
|
66 |
+
convert_system_message_to_human=True
|
67 |
+
)
|
68 |
+
|
69 |
+
def call_model(state: MessagesState):
|
70 |
+
chat_history = state["messages"][:-1]
|
71 |
+
user_input = state["messages"][-1].content
|
72 |
+
|
73 |
+
formatted_messages = self.prompt.format_messages(
|
74 |
+
chat_history=chat_history,
|
75 |
+
input=user_input
|
76 |
+
)
|
77 |
+
|
78 |
+
response = self.model.invoke(formatted_messages)
|
79 |
+
return {"messages": response}
|
80 |
+
|
81 |
+
workflow = StateGraph(state_schema=MessagesState)
|
82 |
+
workflow.add_node("model", call_model)
|
83 |
+
workflow.add_edge(START, "model")
|
84 |
+
|
85 |
+
self.memory = MemorySaver()
|
86 |
+
self.app = workflow.compile(checkpointer=self.memory)
|
87 |
+
|
88 |
+
def get_response(self, user_message, history, thread_id):
|
89 |
+
try:
|
90 |
+
langchain_history = []
|
91 |
+
for user, bot in history:
|
92 |
+
langchain_history.append(HumanMessage(content=user))
|
93 |
+
langchain_history.append(AIMessage(content=bot))
|
94 |
+
|
95 |
+
input_message = HumanMessage(content=user_message)
|
96 |
+
full_history = langchain_history + [input_message]
|
97 |
+
|
98 |
+
full_response = ""
|
99 |
+
config = {"configurable": {"thread_id": thread_id}}
|
100 |
+
|
101 |
+
response = self.app.invoke({"messages": full_history}, config)
|
102 |
+
complete_response = response["messages"][-1].content
|
103 |
+
|
104 |
+
for char in complete_response:
|
105 |
+
full_response += char
|
106 |
+
yield full_response
|
107 |
+
time.sleep(0.01)
|
108 |
+
|
109 |
+
except Exception as e:
|
110 |
+
logger.error(f"LangGraph Error: {e}")
|
111 |
+
yield f"Error: {type(e).__name__} — {str(e)}"
|
112 |
+
|
113 |
+
|
114 |
+
chatbot = GeminiChatbot()
|
115 |
+
sessions = load_all_sessions()
|
116 |
+
|
117 |
+
|
118 |
+
def launch_interface():
|
119 |
+
with gr.Blocks(
|
120 |
+
theme=gr.themes.Base(),
|
121 |
+
css="""
|
122 |
+
body {
|
123 |
+
background-color: black;
|
124 |
+
}
|
125 |
+
.gr-block.gr-textbox textarea {
|
126 |
+
background-color: #2f2f2f;
|
127 |
+
color: white;
|
128 |
+
}
|
129 |
+
.gr-chatbot {
|
130 |
+
background-color: #2f2f2f;
|
131 |
+
color: white;
|
132 |
+
}
|
133 |
+
.gr-button, .gr-dropdown {
|
134 |
+
margin: 5px auto;
|
135 |
+
display: block;
|
136 |
+
width: 50%;
|
137 |
+
}
|
138 |
+
.gr-markdown h2 {
|
139 |
+
text-align: center;
|
140 |
+
color: white;
|
141 |
+
}
|
142 |
+
"""
|
143 |
+
) as demo:
|
144 |
+
demo.title = "LangChain Powered ChatBot"
|
145 |
+
gr.Markdown("## LangChain Powered ChatBot")
|
146 |
+
|
147 |
+
current_thread_id = gr.State()
|
148 |
+
session_names = gr.State()
|
149 |
+
history = gr.State([])
|
150 |
+
|
151 |
+
if not sessions:
|
152 |
+
new_id = str(uuid.uuid4())
|
153 |
+
sessions[new_id] = []
|
154 |
+
save_all_sessions(sessions)
|
155 |
+
current_thread_id.value = new_id
|
156 |
+
session_names.value = [f"NEW: {new_id}"]
|
157 |
+
else:
|
158 |
+
current_thread_id.value = next(iter(sessions.keys()))
|
159 |
+
session_names.value = [f"PREVIOUS: {k}" for k in sessions.keys()]
|
160 |
+
|
161 |
+
def get_dropdown_choices():
|
162 |
+
choices = []
|
163 |
+
for session_id in sessions:
|
164 |
+
if sessions[session_id]:
|
165 |
+
choices.append(f"PREVIOUS: {session_id}")
|
166 |
+
choices.append(f"NEW: {current_thread_id.value}")
|
167 |
+
return choices
|
168 |
+
|
169 |
+
with gr.Column():
|
170 |
+
new_chat_btn = gr.Button("New Chat", variant="primary")
|
171 |
+
session_selector = gr.Dropdown(
|
172 |
+
label="Chats",
|
173 |
+
choices=get_dropdown_choices(),
|
174 |
+
value=f"NEW: {current_thread_id.value}",
|
175 |
+
interactive=True
|
176 |
+
)
|
177 |
+
|
178 |
+
chatbot_ui = gr.Chatbot(label="Conversation", height=320)
|
179 |
+
|
180 |
+
with gr.Row():
|
181 |
+
msg = gr.Textbox(placeholder="Ask a question...", container=False, scale=9)
|
182 |
+
send = gr.Button("Send", variant="primary", scale=1)
|
183 |
+
|
184 |
+
clear = gr.Button("Clear Current Chat")
|
185 |
+
|
186 |
+
def start_new_chat():
|
187 |
+
new_id = str(uuid.uuid4())
|
188 |
+
sessions[new_id] = []
|
189 |
+
save_all_sessions(sessions)
|
190 |
+
|
191 |
+
display_name = f"NEW: {new_id}"
|
192 |
+
updated_choices = [f"PREVIOUS: {k}" for k in sessions if sessions[k]] + [display_name]
|
193 |
+
|
194 |
+
return (
|
195 |
+
new_id,
|
196 |
+
[],
|
197 |
+
gr.update(choices=updated_choices, value=display_name),
|
198 |
+
display_name
|
199 |
+
)
|
200 |
+
|
201 |
+
|
202 |
+
|
203 |
+
def switch_chat(selected_display_id):
|
204 |
+
if not selected_display_id:
|
205 |
+
return current_thread_id.value, [], ""
|
206 |
+
|
207 |
+
true_id = selected_display_id.split(": ", 1)[-1]
|
208 |
+
chat_history = sessions.get(true_id, [])
|
209 |
+
return true_id, chat_history, selected_display_id
|
210 |
+
|
211 |
+
def respond(message, history, thread_id):
|
212 |
+
if not message.strip():
|
213 |
+
yield history
|
214 |
+
return
|
215 |
+
|
216 |
+
history.append((message, ""))
|
217 |
+
yield history
|
218 |
+
|
219 |
+
full_response = ""
|
220 |
+
for chunk in chatbot.get_response(message, history[:-1], thread_id):
|
221 |
+
full_response = chunk
|
222 |
+
history[-1] = (message, full_response)
|
223 |
+
yield history
|
224 |
+
|
225 |
+
sessions[thread_id] = history
|
226 |
+
save_all_sessions(sessions)
|
227 |
+
|
228 |
+
def clear_current(thread_id):
|
229 |
+
sessions[thread_id] = []
|
230 |
+
save_all_sessions(sessions)
|
231 |
+
return []
|
232 |
+
|
233 |
+
new_chat_btn.click(
|
234 |
+
start_new_chat,
|
235 |
+
outputs=[current_thread_id, chatbot_ui, session_selector, session_selector]
|
236 |
+
)
|
237 |
+
|
238 |
+
session_selector.change(
|
239 |
+
switch_chat,
|
240 |
+
inputs=session_selector,
|
241 |
+
outputs=[current_thread_id, chatbot_ui, session_selector]
|
242 |
+
)
|
243 |
+
|
244 |
+
send.click(
|
245 |
+
respond,
|
246 |
+
inputs=[msg, chatbot_ui, current_thread_id],
|
247 |
+
outputs=[chatbot_ui]
|
248 |
+
).then(
|
249 |
+
lambda: "", None, msg
|
250 |
+
)
|
251 |
+
|
252 |
+
msg.submit(
|
253 |
+
respond,
|
254 |
+
inputs=[msg, chatbot_ui, current_thread_id],
|
255 |
+
outputs=[chatbot_ui]
|
256 |
+
).then(
|
257 |
+
lambda: "", None, msg
|
258 |
+
)
|
259 |
+
|
260 |
+
clear.click(
|
261 |
+
clear_current,
|
262 |
+
inputs=[current_thread_id],
|
263 |
+
outputs=[chatbot_ui]
|
264 |
+
)
|
265 |
+
|
266 |
+
return demo
|
267 |
+
|
268 |
+
|
269 |
+
|
270 |
+
if __name__ == "__main__":
|
271 |
+
try:
|
272 |
+
demo = launch_interface()
|
273 |
+
demo.launch(share=True)
|
274 |
+
except Exception as e:
|
275 |
+
logger.critical(f"App failed: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|