Update app.py
Browse files
app.py
CHANGED
|
@@ -2,10 +2,10 @@ from typing import TypedDict, List, Union
|
|
| 2 |
from langgraph.graph import StateGraph, START, END
|
| 3 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 4 |
from langchain_core.messages import HumanMessage, AIMessage
|
|
|
|
| 5 |
from dotenv import load_dotenv, find_dotenv
|
| 6 |
import gradio as gr
|
| 7 |
import os
|
| 8 |
-
from langchain_groq import ChatGroq
|
| 9 |
|
| 10 |
# Load environment variables
|
| 11 |
load_dotenv(find_dotenv())
|
|
@@ -103,201 +103,3 @@ if __name__ == "__main__":
|
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
-
# from typing import TypedDict, List, Union
|
| 107 |
-
# from langgraph.graph import StateGraph, START, END
|
| 108 |
-
# from langchain_google_genai import ChatGoogleGenerativeAI
|
| 109 |
-
# from langchain_core.messages import HumanMessage, AIMessage
|
| 110 |
-
# from dotenv import load_dotenv, find_dotenv
|
| 111 |
-
# import gradio as gr
|
| 112 |
-
# import os
|
| 113 |
-
|
| 114 |
-
# # Load environment variables
|
| 115 |
-
# load_dotenv(find_dotenv())
|
| 116 |
-
|
| 117 |
-
# # Define Agent State
|
| 118 |
-
# class AgentState(TypedDict):
|
| 119 |
-
# messages: List[Union[HumanMessage, AIMessage]]
|
| 120 |
-
|
| 121 |
-
# # Initialize Gemini model
|
| 122 |
-
# llm = ChatGoogleGenerativeAI(
|
| 123 |
-
# model="models/gemini-1.5-flash-latest",
|
| 124 |
-
# temperature=1
|
| 125 |
-
# )
|
| 126 |
-
|
| 127 |
-
# # LangGraph processing node
|
| 128 |
-
# def process(state: AgentState) -> AgentState:
|
| 129 |
-
# response = llm.invoke(state["messages"])
|
| 130 |
-
# state["messages"].append(AIMessage(content=response.content))
|
| 131 |
-
# return state
|
| 132 |
-
|
| 133 |
-
# # Setup LangGraph
|
| 134 |
-
# graph = StateGraph(AgentState)
|
| 135 |
-
# graph.add_node("process", process)
|
| 136 |
-
# graph.add_edge(START, "process")
|
| 137 |
-
# graph.add_edge("process", END)
|
| 138 |
-
# agent = graph.compile()
|
| 139 |
-
|
| 140 |
-
# # Global conversation history
|
| 141 |
-
# conversation_history = []
|
| 142 |
-
|
| 143 |
-
# # Chat handler
|
| 144 |
-
# def chat_handler(user_input, history):
|
| 145 |
-
# global conversation_history
|
| 146 |
-
|
| 147 |
-
# # Add user message
|
| 148 |
-
# conversation_history.append(HumanMessage(content=user_input))
|
| 149 |
-
|
| 150 |
-
# # If too many HumanMessages, remove oldest one
|
| 151 |
-
# human_messages = [m for m in conversation_history if isinstance(m, HumanMessage)]
|
| 152 |
-
# if len(human_messages) > 10:
|
| 153 |
-
# for i, m in enumerate(conversation_history):
|
| 154 |
-
# if isinstance(m, HumanMessage):
|
| 155 |
-
# del conversation_history[i]
|
| 156 |
-
# break
|
| 157 |
-
|
| 158 |
-
# # Get AI response
|
| 159 |
-
# result = agent.invoke({"messages": conversation_history})
|
| 160 |
-
# conversation_history = result["messages"]
|
| 161 |
-
# reply = conversation_history[-1].content
|
| 162 |
-
|
| 163 |
-
# # Append for UI
|
| 164 |
-
# history = history or []
|
| 165 |
-
# history.append({"role": "user", "content": user_input})
|
| 166 |
-
# history.append({"role": "assistant", "content": reply})
|
| 167 |
-
# return history, history
|
| 168 |
-
|
| 169 |
-
# # Save conversation to file
|
| 170 |
-
# def save_chat():
|
| 171 |
-
# with open("logging.txt", "w") as file:
|
| 172 |
-
# file.write("Your Conversation Log:\n\n")
|
| 173 |
-
# for msg in conversation_history:
|
| 174 |
-
# if isinstance(msg, HumanMessage):
|
| 175 |
-
# file.write(f"You: {msg.content}\n")
|
| 176 |
-
# elif isinstance(msg, AIMessage):
|
| 177 |
-
# file.write(f"AI: {msg.content}\n\n")
|
| 178 |
-
# file.write("End of Conversation\n")
|
| 179 |
-
# return "logging.txt"
|
| 180 |
-
|
| 181 |
-
# # Gradio UI
|
| 182 |
-
# with gr.Blocks() as demo:
|
| 183 |
-
# gr.Markdown("<h1 style='text-align: center;'>LangGraph Gemini Chatbot</h1>")
|
| 184 |
-
|
| 185 |
-
# chatbot = gr.Chatbot(label="Chat", type="messages")
|
| 186 |
-
# msg = gr.Textbox(placeholder="Type your message...", label="Your Message")
|
| 187 |
-
|
| 188 |
-
# with gr.Row():
|
| 189 |
-
# send_btn = gr.Button("Send")
|
| 190 |
-
# save_btn = gr.Button("Download Chat")
|
| 191 |
-
# file_output = gr.File(label="Download", visible=False)
|
| 192 |
-
|
| 193 |
-
# state = gr.State([])
|
| 194 |
-
|
| 195 |
-
# send_btn.click(chat_handler, inputs=[msg, state], outputs=[chatbot, state])
|
| 196 |
-
# msg.submit(chat_handler, inputs=[msg, state], outputs=[chatbot, state])
|
| 197 |
-
|
| 198 |
-
# save_btn.click(fn=save_chat, outputs=file_output)
|
| 199 |
-
|
| 200 |
-
# if __name__ == "__main__":
|
| 201 |
-
# demo.launch()
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
# from typing import TypedDict, List, Union
|
| 208 |
-
# from langgraph.graph import StateGraph, START, END
|
| 209 |
-
# from langchain_google_genai import ChatGoogleGenerativeAI
|
| 210 |
-
# from langchain_core.messages import HumanMessage, AIMessage
|
| 211 |
-
# from dotenv import load_dotenv, find_dotenv
|
| 212 |
-
# import gradio as gr
|
| 213 |
-
# import os
|
| 214 |
-
|
| 215 |
-
# # Load environment variables
|
| 216 |
-
# load_dotenv(find_dotenv())
|
| 217 |
-
|
| 218 |
-
# # Define Agent State
|
| 219 |
-
# class AgentState(TypedDict):
|
| 220 |
-
# messages: List[Union[HumanMessage, AIMessage]]
|
| 221 |
-
|
| 222 |
-
# # Initialize Gemini model
|
| 223 |
-
# llm = ChatGoogleGenerativeAI(
|
| 224 |
-
# model="models/gemini-1.5-flash-latest",
|
| 225 |
-
# temperature=1
|
| 226 |
-
# )
|
| 227 |
-
|
| 228 |
-
# # Processing node
|
| 229 |
-
# def process(state: AgentState) -> AgentState:
|
| 230 |
-
# response = llm.invoke(state["messages"])
|
| 231 |
-
# state["messages"].append(AIMessage(content=response.content))
|
| 232 |
-
# print(f"\nAI: {response.content}")
|
| 233 |
-
# return state
|
| 234 |
-
|
| 235 |
-
# # LangGraph setup
|
| 236 |
-
# graph = StateGraph(AgentState)
|
| 237 |
-
# graph.add_node("process", process)
|
| 238 |
-
# graph.add_edge(START, "process")
|
| 239 |
-
# graph.add_edge("process", END)
|
| 240 |
-
# agent = graph.compile()
|
| 241 |
-
|
| 242 |
-
# # Global chat history
|
| 243 |
-
# conversation_history = []
|
| 244 |
-
|
| 245 |
-
# # Gradio Chat Handler
|
| 246 |
-
# def chat_handler(message, history):
|
| 247 |
-
# global conversation_history
|
| 248 |
-
|
| 249 |
-
# # Append user message
|
| 250 |
-
# conversation_history.append(HumanMessage(content=message))
|
| 251 |
-
|
| 252 |
-
# # Invoke agent
|
| 253 |
-
# result = agent.invoke({"messages": conversation_history})
|
| 254 |
-
# conversation_history = result["messages"]
|
| 255 |
-
# response = conversation_history[-1].content
|
| 256 |
-
|
| 257 |
-
# # Append to chat UI history
|
| 258 |
-
# history = history or []
|
| 259 |
-
# history.append((message, response))
|
| 260 |
-
# return history, history
|
| 261 |
-
|
| 262 |
-
# # Download handler
|
| 263 |
-
# def download_chat():
|
| 264 |
-
# text = ""
|
| 265 |
-
# for msg in conversation_history:
|
| 266 |
-
# if isinstance(msg, HumanMessage):
|
| 267 |
-
# text += f"User: {msg.content}\n"
|
| 268 |
-
# elif isinstance(msg, AIMessage):
|
| 269 |
-
# text += f"Bot: {msg.content}\n"
|
| 270 |
-
# return text
|
| 271 |
-
|
| 272 |
-
# # Gradio UI
|
| 273 |
-
# with gr.Blocks(title="LangGraph Gemini Chatbot") as demo:
|
| 274 |
-
# gr.Markdown("<h1 style='text-align: center;'> LangGraph Gemini Chatbot</h1>")
|
| 275 |
-
|
| 276 |
-
# chatbot = gr.Chatbot(label="Chat")
|
| 277 |
-
# msg = gr.Textbox(placeholder="Type your message here...", label="Your Message")
|
| 278 |
-
|
| 279 |
-
# with gr.Row():
|
| 280 |
-
# send_btn = gr.Button("Send")
|
| 281 |
-
# download_btn = gr.Button("Download Chat History (.txt)")
|
| 282 |
-
# download_file = gr.File(label="Download", visible=False)
|
| 283 |
-
|
| 284 |
-
# state = gr.State([])
|
| 285 |
-
|
| 286 |
-
# def handle_send(user_input, history):
|
| 287 |
-
# return chat_handler(user_input, history)
|
| 288 |
-
|
| 289 |
-
# send_btn.click(handle_send, inputs=[msg, state], outputs=[chatbot, state])
|
| 290 |
-
# msg.submit(handle_send, inputs=[msg, state], outputs=[chatbot, state])
|
| 291 |
-
|
| 292 |
-
# def handle_download():
|
| 293 |
-
# txt = download_chat()
|
| 294 |
-
# path = "chat_history.txt"
|
| 295 |
-
# with open(path, "w") as f:
|
| 296 |
-
# f.write(txt)
|
| 297 |
-
# return gr.File.update(value=path, visible=True)
|
| 298 |
-
|
| 299 |
-
# download_btn.click(fn=handle_download, outputs=download_file)
|
| 300 |
-
|
| 301 |
-
# # Launch the app
|
| 302 |
-
# if __name__ == "__main__":
|
| 303 |
-
# demo.launch()
|
|
|
|
| 2 |
from langgraph.graph import StateGraph, START, END
|
| 3 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 4 |
from langchain_core.messages import HumanMessage, AIMessage
|
| 5 |
+
from langchain_groq import ChatGroq
|
| 6 |
from dotenv import load_dotenv, find_dotenv
|
| 7 |
import gradio as gr
|
| 8 |
import os
|
|
|
|
| 9 |
|
| 10 |
# Load environment variables
|
| 11 |
load_dotenv(find_dotenv())
|
|
|
|
| 103 |
|
| 104 |
|
| 105 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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