Update app.py
Browse files
app.py
CHANGED
|
@@ -6,26 +6,26 @@ from dotenv import load_dotenv, find_dotenv
|
|
| 6 |
import gradio as gr
|
| 7 |
import os
|
| 8 |
|
| 9 |
-
# Load
|
| 10 |
load_dotenv(find_dotenv())
|
| 11 |
|
| 12 |
-
# Define
|
| 13 |
class AgentState(TypedDict):
|
| 14 |
messages: List[Union[HumanMessage, AIMessage]]
|
| 15 |
|
| 16 |
-
# Initialize
|
| 17 |
llm = ChatGoogleGenerativeAI(
|
| 18 |
model="models/gemini-1.5-flash-latest",
|
| 19 |
temperature=1
|
| 20 |
)
|
| 21 |
|
| 22 |
-
# LangGraph node
|
| 23 |
def process(state: AgentState) -> AgentState:
|
| 24 |
response = llm.invoke(state["messages"])
|
| 25 |
state["messages"].append(AIMessage(content=response.content))
|
| 26 |
return state
|
| 27 |
|
| 28 |
-
#
|
| 29 |
graph = StateGraph(AgentState)
|
| 30 |
graph.add_node("process", process)
|
| 31 |
graph.add_edge(START, "process")
|
|
@@ -35,64 +35,62 @@ agent = graph.compile()
|
|
| 35 |
# Global conversation history
|
| 36 |
conversation_history = []
|
| 37 |
|
| 38 |
-
#
|
| 39 |
def chat_handler(user_input, history):
|
| 40 |
global conversation_history
|
| 41 |
|
| 42 |
-
#
|
| 43 |
conversation_history.append(HumanMessage(content=user_input))
|
| 44 |
|
| 45 |
-
#
|
| 46 |
-
|
| 47 |
-
if
|
| 48 |
for i, m in enumerate(conversation_history):
|
| 49 |
if isinstance(m, HumanMessage):
|
| 50 |
del conversation_history[i]
|
| 51 |
-
break
|
| 52 |
|
| 53 |
-
#
|
| 54 |
result = agent.invoke({"messages": conversation_history})
|
| 55 |
conversation_history = result["messages"]
|
| 56 |
-
|
| 57 |
|
| 58 |
-
# Append
|
| 59 |
history = history or []
|
| 60 |
-
history.append(
|
|
|
|
| 61 |
return history, history
|
| 62 |
|
| 63 |
-
#
|
| 64 |
-
def
|
| 65 |
with open("logging.txt", "w") as file:
|
| 66 |
file.write("Your Conversation Log:\n\n")
|
| 67 |
-
for
|
| 68 |
-
if isinstance(
|
| 69 |
-
file.write(f"You: {
|
| 70 |
-
elif isinstance(
|
| 71 |
-
file.write(f"AI: {
|
| 72 |
file.write("End of Conversation\n")
|
| 73 |
return "logging.txt"
|
| 74 |
|
| 75 |
# Gradio UI
|
| 76 |
-
with gr.Blocks(
|
| 77 |
-
gr.Markdown("<h1 style='text-align: center;'>
|
| 78 |
|
| 79 |
-
chatbot = gr.Chatbot(label="Chat")
|
| 80 |
-
msg = gr.Textbox(placeholder="Type your message
|
| 81 |
|
| 82 |
with gr.Row():
|
| 83 |
send_btn = gr.Button("Send")
|
| 84 |
-
save_btn = gr.Button("
|
|
|
|
| 85 |
|
| 86 |
-
file_output = gr.File(label="Click to download", visible=False)
|
| 87 |
state = gr.State([])
|
| 88 |
|
| 89 |
-
# Bind chat
|
| 90 |
send_btn.click(chat_handler, inputs=[msg, state], outputs=[chatbot, state])
|
| 91 |
msg.submit(chat_handler, inputs=[msg, state], outputs=[chatbot, state])
|
| 92 |
|
| 93 |
-
|
| 94 |
-
save_btn.click(fn=download_chat_log, outputs=file_output)
|
| 95 |
-
file_output.render()
|
| 96 |
|
| 97 |
if __name__ == "__main__":
|
| 98 |
demo.launch()
|
|
@@ -100,6 +98,7 @@ if __name__ == "__main__":
|
|
| 100 |
|
| 101 |
|
| 102 |
|
|
|
|
| 103 |
# from typing import TypedDict, List, Union
|
| 104 |
# from langgraph.graph import StateGraph, START, END
|
| 105 |
# from langchain_google_genai import ChatGoogleGenerativeAI
|
|
|
|
| 6 |
import gradio as gr
|
| 7 |
import os
|
| 8 |
|
| 9 |
+
# Load environment variables
|
| 10 |
load_dotenv(find_dotenv())
|
| 11 |
|
| 12 |
+
# Define Agent State
|
| 13 |
class AgentState(TypedDict):
|
| 14 |
messages: List[Union[HumanMessage, AIMessage]]
|
| 15 |
|
| 16 |
+
# Initialize Gemini model
|
| 17 |
llm = ChatGoogleGenerativeAI(
|
| 18 |
model="models/gemini-1.5-flash-latest",
|
| 19 |
temperature=1
|
| 20 |
)
|
| 21 |
|
| 22 |
+
# LangGraph processing node
|
| 23 |
def process(state: AgentState) -> AgentState:
|
| 24 |
response = llm.invoke(state["messages"])
|
| 25 |
state["messages"].append(AIMessage(content=response.content))
|
| 26 |
return state
|
| 27 |
|
| 28 |
+
# Setup LangGraph
|
| 29 |
graph = StateGraph(AgentState)
|
| 30 |
graph.add_node("process", process)
|
| 31 |
graph.add_edge(START, "process")
|
|
|
|
| 35 |
# Global conversation history
|
| 36 |
conversation_history = []
|
| 37 |
|
| 38 |
+
# Chat handler
|
| 39 |
def chat_handler(user_input, history):
|
| 40 |
global conversation_history
|
| 41 |
|
| 42 |
+
# Add user message
|
| 43 |
conversation_history.append(HumanMessage(content=user_input))
|
| 44 |
|
| 45 |
+
# If too many HumanMessages, remove oldest one
|
| 46 |
+
human_messages = [m for m in conversation_history if isinstance(m, HumanMessage)]
|
| 47 |
+
if len(human_messages) > 10:
|
| 48 |
for i, m in enumerate(conversation_history):
|
| 49 |
if isinstance(m, HumanMessage):
|
| 50 |
del conversation_history[i]
|
| 51 |
+
break
|
| 52 |
|
| 53 |
+
# Get AI response
|
| 54 |
result = agent.invoke({"messages": conversation_history})
|
| 55 |
conversation_history = result["messages"]
|
| 56 |
+
reply = conversation_history[-1].content
|
| 57 |
|
| 58 |
+
# Append for UI
|
| 59 |
history = history or []
|
| 60 |
+
history.append({"role": "user", "content": user_input})
|
| 61 |
+
history.append({"role": "assistant", "content": reply})
|
| 62 |
return history, history
|
| 63 |
|
| 64 |
+
# Save conversation to file
|
| 65 |
+
def save_chat():
|
| 66 |
with open("logging.txt", "w") as file:
|
| 67 |
file.write("Your Conversation Log:\n\n")
|
| 68 |
+
for msg in conversation_history:
|
| 69 |
+
if isinstance(msg, HumanMessage):
|
| 70 |
+
file.write(f"You: {msg.content}\n")
|
| 71 |
+
elif isinstance(msg, AIMessage):
|
| 72 |
+
file.write(f"AI: {msg.content}\n\n")
|
| 73 |
file.write("End of Conversation\n")
|
| 74 |
return "logging.txt"
|
| 75 |
|
| 76 |
# Gradio UI
|
| 77 |
+
with gr.Blocks() as demo:
|
| 78 |
+
gr.Markdown("<h1 style='text-align: center;'>LangGraph Gemini Chatbot</h1>")
|
| 79 |
|
| 80 |
+
chatbot = gr.Chatbot(label="Chat", type="messages")
|
| 81 |
+
msg = gr.Textbox(placeholder="Type your message...", label="Your Message")
|
| 82 |
|
| 83 |
with gr.Row():
|
| 84 |
send_btn = gr.Button("Send")
|
| 85 |
+
save_btn = gr.Button("Download Chat")
|
| 86 |
+
file_output = gr.File(label="Download", visible=False)
|
| 87 |
|
|
|
|
| 88 |
state = gr.State([])
|
| 89 |
|
|
|
|
| 90 |
send_btn.click(chat_handler, inputs=[msg, state], outputs=[chatbot, state])
|
| 91 |
msg.submit(chat_handler, inputs=[msg, state], outputs=[chatbot, state])
|
| 92 |
|
| 93 |
+
save_btn.click(fn=save_chat, outputs=file_output)
|
|
|
|
|
|
|
| 94 |
|
| 95 |
if __name__ == "__main__":
|
| 96 |
demo.launch()
|
|
|
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
+
|
| 102 |
# from typing import TypedDict, List, Union
|
| 103 |
# from langgraph.graph import StateGraph, START, END
|
| 104 |
# from langchain_google_genai import ChatGoogleGenerativeAI
|