Update app.py
Browse files
app.py
CHANGED
|
@@ -6,62 +6,71 @@ 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 |
-
#
|
| 23 |
def process(state: AgentState) -> AgentState:
|
| 24 |
response = llm.invoke(state["messages"])
|
| 25 |
state["messages"].append(AIMessage(content=response.content))
|
| 26 |
-
print(f"\nAI: {response.content}")
|
| 27 |
return state
|
| 28 |
|
| 29 |
-
#
|
| 30 |
graph = StateGraph(AgentState)
|
| 31 |
graph.add_node("process", process)
|
| 32 |
graph.add_edge(START, "process")
|
| 33 |
graph.add_edge("process", END)
|
| 34 |
agent = graph.compile()
|
| 35 |
|
| 36 |
-
# Global
|
| 37 |
conversation_history = []
|
| 38 |
|
| 39 |
-
# Gradio
|
| 40 |
-
def chat_handler(
|
| 41 |
global conversation_history
|
| 42 |
|
| 43 |
-
# Append
|
| 44 |
-
conversation_history.append(HumanMessage(content=
|
| 45 |
|
| 46 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
result = agent.invoke({"messages": conversation_history})
|
| 48 |
conversation_history = result["messages"]
|
| 49 |
response = conversation_history[-1].content
|
| 50 |
|
| 51 |
-
# Append to
|
| 52 |
history = history or []
|
| 53 |
-
history.append((
|
| 54 |
return history, history
|
| 55 |
|
| 56 |
-
# Download
|
| 57 |
-
def
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
| 65 |
|
| 66 |
# Gradio UI
|
| 67 |
with gr.Blocks(title="LangGraph Gemini Chatbot") as demo:
|
|
@@ -72,26 +81,119 @@ with gr.Blocks(title="LangGraph Gemini Chatbot") as demo:
|
|
| 72 |
|
| 73 |
with gr.Row():
|
| 74 |
send_btn = gr.Button("Send")
|
| 75 |
-
|
| 76 |
-
download_file = gr.File(label="Download", visible=False)
|
| 77 |
|
|
|
|
| 78 |
state = gr.State([])
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
send_btn.click(handle_send, inputs=[msg, state], outputs=[chatbot, state])
|
| 84 |
-
msg.submit(handle_send, inputs=[msg, state], outputs=[chatbot, state])
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
with open(path, "w") as f:
|
| 90 |
-
f.write(txt)
|
| 91 |
-
return gr.File.update(value=path, visible=True)
|
| 92 |
|
| 93 |
-
download_btn.click(fn=handle_download, outputs=download_file)
|
| 94 |
-
|
| 95 |
-
# Launch the app
|
| 96 |
if __name__ == "__main__":
|
| 97 |
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
import gradio as gr
|
| 7 |
import os
|
| 8 |
|
| 9 |
+
# Load .env variables
|
| 10 |
load_dotenv(find_dotenv())
|
| 11 |
|
| 12 |
+
# Define state type
|
| 13 |
class AgentState(TypedDict):
|
| 14 |
messages: List[Union[HumanMessage, AIMessage]]
|
| 15 |
|
| 16 |
+
# Initialize LLM
|
| 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 |
+
# Build the LangGraph
|
| 29 |
graph = StateGraph(AgentState)
|
| 30 |
graph.add_node("process", process)
|
| 31 |
graph.add_edge(START, "process")
|
| 32 |
graph.add_edge("process", END)
|
| 33 |
agent = graph.compile()
|
| 34 |
|
| 35 |
+
# Global conversation history
|
| 36 |
conversation_history = []
|
| 37 |
|
| 38 |
+
# Gradio chat function
|
| 39 |
+
def chat_handler(user_input, history):
|
| 40 |
global conversation_history
|
| 41 |
|
| 42 |
+
# Append new human message
|
| 43 |
+
conversation_history.append(HumanMessage(content=user_input))
|
| 44 |
|
| 45 |
+
# Count HumanMessages and trim oldest if > 10
|
| 46 |
+
human_count = sum(isinstance(m, HumanMessage) for m in conversation_history)
|
| 47 |
+
if human_count > 10:
|
| 48 |
+
for i, m in enumerate(conversation_history):
|
| 49 |
+
if isinstance(m, HumanMessage):
|
| 50 |
+
del conversation_history[i]
|
| 51 |
+
break # Remove only the oldest one
|
| 52 |
+
|
| 53 |
+
# Run through LangGraph
|
| 54 |
result = agent.invoke({"messages": conversation_history})
|
| 55 |
conversation_history = result["messages"]
|
| 56 |
response = conversation_history[-1].content
|
| 57 |
|
| 58 |
+
# Append to UI chat history
|
| 59 |
history = history or []
|
| 60 |
+
history.append((user_input, response))
|
| 61 |
return history, history
|
| 62 |
|
| 63 |
+
# Download chat as logging.txt
|
| 64 |
+
def download_chat_log():
|
| 65 |
+
with open("logging.txt", "w") as file:
|
| 66 |
+
file.write("Your Conversation Log:\n\n")
|
| 67 |
+
for message in conversation_history:
|
| 68 |
+
if isinstance(message, HumanMessage):
|
| 69 |
+
file.write(f"You: {message.content}\n")
|
| 70 |
+
elif isinstance(message, AIMessage):
|
| 71 |
+
file.write(f"AI: {message.content}\n\n")
|
| 72 |
+
file.write("End of Conversation\n")
|
| 73 |
+
return "logging.txt"
|
| 74 |
|
| 75 |
# Gradio UI
|
| 76 |
with gr.Blocks(title="LangGraph Gemini Chatbot") as demo:
|
|
|
|
| 81 |
|
| 82 |
with gr.Row():
|
| 83 |
send_btn = gr.Button("Send")
|
| 84 |
+
save_btn = gr.Button("Save Chat as logging.txt")
|
|
|
|
| 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 |
+
# Bind download
|
| 94 |
+
save_btn.click(fn=download_chat_log, outputs=file_output)
|
| 95 |
+
file_output.render()
|
|
|
|
|
|
|
|
|
|
| 96 |
|
|
|
|
|
|
|
|
|
|
| 97 |
if __name__ == "__main__":
|
| 98 |
demo.launch()
|
| 99 |
+
|
| 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
|
| 106 |
+
# from langchain_core.messages import HumanMessage, AIMessage
|
| 107 |
+
# from dotenv import load_dotenv, find_dotenv
|
| 108 |
+
# import gradio as gr
|
| 109 |
+
# import os
|
| 110 |
+
|
| 111 |
+
# # Load environment variables
|
| 112 |
+
# load_dotenv(find_dotenv())
|
| 113 |
+
|
| 114 |
+
# # Define Agent State
|
| 115 |
+
# class AgentState(TypedDict):
|
| 116 |
+
# messages: List[Union[HumanMessage, AIMessage]]
|
| 117 |
+
|
| 118 |
+
# # Initialize Gemini model
|
| 119 |
+
# llm = ChatGoogleGenerativeAI(
|
| 120 |
+
# model="models/gemini-1.5-flash-latest",
|
| 121 |
+
# temperature=1
|
| 122 |
+
# )
|
| 123 |
+
|
| 124 |
+
# # Processing node
|
| 125 |
+
# def process(state: AgentState) -> AgentState:
|
| 126 |
+
# response = llm.invoke(state["messages"])
|
| 127 |
+
# state["messages"].append(AIMessage(content=response.content))
|
| 128 |
+
# print(f"\nAI: {response.content}")
|
| 129 |
+
# return state
|
| 130 |
+
|
| 131 |
+
# # LangGraph setup
|
| 132 |
+
# graph = StateGraph(AgentState)
|
| 133 |
+
# graph.add_node("process", process)
|
| 134 |
+
# graph.add_edge(START, "process")
|
| 135 |
+
# graph.add_edge("process", END)
|
| 136 |
+
# agent = graph.compile()
|
| 137 |
+
|
| 138 |
+
# # Global chat history
|
| 139 |
+
# conversation_history = []
|
| 140 |
+
|
| 141 |
+
# # Gradio Chat Handler
|
| 142 |
+
# def chat_handler(message, history):
|
| 143 |
+
# global conversation_history
|
| 144 |
+
|
| 145 |
+
# # Append user message
|
| 146 |
+
# conversation_history.append(HumanMessage(content=message))
|
| 147 |
+
|
| 148 |
+
# # Invoke agent
|
| 149 |
+
# result = agent.invoke({"messages": conversation_history})
|
| 150 |
+
# conversation_history = result["messages"]
|
| 151 |
+
# response = conversation_history[-1].content
|
| 152 |
+
|
| 153 |
+
# # Append to chat UI history
|
| 154 |
+
# history = history or []
|
| 155 |
+
# history.append((message, response))
|
| 156 |
+
# return history, history
|
| 157 |
+
|
| 158 |
+
# # Download handler
|
| 159 |
+
# def download_chat():
|
| 160 |
+
# text = ""
|
| 161 |
+
# for msg in conversation_history:
|
| 162 |
+
# if isinstance(msg, HumanMessage):
|
| 163 |
+
# text += f"User: {msg.content}\n"
|
| 164 |
+
# elif isinstance(msg, AIMessage):
|
| 165 |
+
# text += f"Bot: {msg.content}\n"
|
| 166 |
+
# return text
|
| 167 |
+
|
| 168 |
+
# # Gradio UI
|
| 169 |
+
# with gr.Blocks(title="LangGraph Gemini Chatbot") as demo:
|
| 170 |
+
# gr.Markdown("<h1 style='text-align: center;'> LangGraph Gemini Chatbot</h1>")
|
| 171 |
+
|
| 172 |
+
# chatbot = gr.Chatbot(label="Chat")
|
| 173 |
+
# msg = gr.Textbox(placeholder="Type your message here...", label="Your Message")
|
| 174 |
+
|
| 175 |
+
# with gr.Row():
|
| 176 |
+
# send_btn = gr.Button("Send")
|
| 177 |
+
# download_btn = gr.Button("Download Chat History (.txt)")
|
| 178 |
+
# download_file = gr.File(label="Download", visible=False)
|
| 179 |
+
|
| 180 |
+
# state = gr.State([])
|
| 181 |
+
|
| 182 |
+
# def handle_send(user_input, history):
|
| 183 |
+
# return chat_handler(user_input, history)
|
| 184 |
+
|
| 185 |
+
# send_btn.click(handle_send, inputs=[msg, state], outputs=[chatbot, state])
|
| 186 |
+
# msg.submit(handle_send, inputs=[msg, state], outputs=[chatbot, state])
|
| 187 |
+
|
| 188 |
+
# def handle_download():
|
| 189 |
+
# txt = download_chat()
|
| 190 |
+
# path = "chat_history.txt"
|
| 191 |
+
# with open(path, "w") as f:
|
| 192 |
+
# f.write(txt)
|
| 193 |
+
# return gr.File.update(value=path, visible=True)
|
| 194 |
+
|
| 195 |
+
# download_btn.click(fn=handle_download, outputs=download_file)
|
| 196 |
+
|
| 197 |
+
# # Launch the app
|
| 198 |
+
# if __name__ == "__main__":
|
| 199 |
+
# demo.launch()
|