Spaces:
Sleeping
Sleeping
Aniket commited on
Commit Β·
3e543ac
1
Parent(s): 764923a
Add application file
Browse files
app.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from langchain_groq import ChatGroq
|
| 3 |
+
from langchain_community.utilities import ArxivAPIWrapper, WikipediaAPIWrapper
|
| 4 |
+
from langchain_community.tools import ArxivQueryRun, WikipediaQueryRun, DuckDuckGoSearchRun
|
| 5 |
+
from langchain.agents import initialize_agent, AgentType
|
| 6 |
+
from langchain.callbacks import StdOutCallbackHandler
|
| 7 |
+
import os
|
| 8 |
+
|
| 9 |
+
# Tools Setup (identical to original)
|
| 10 |
+
arxiv_wrapper = ArxivAPIWrapper(top_k_results=1, doc_content_chars_max=200)
|
| 11 |
+
arxiv = ArxivQueryRun(api_wrapper=arxiv_wrapper)
|
| 12 |
+
wiki_wrapper = WikipediaAPIWrapper(top_k_results=1, doc_content_chars_max=200)
|
| 13 |
+
wiki = WikipediaQueryRun(api_wrapper=wiki_wrapper)
|
| 14 |
+
search = DuckDuckGoSearchRun(name="Search")
|
| 15 |
+
|
| 16 |
+
# Gradio Chat Interface - UPDATED VERSION
|
| 17 |
+
def respond(message, chat_history, api_key):
|
| 18 |
+
if not api_key:
|
| 19 |
+
chat_history.append((message, "β οΈ Please enter your Groq API key"))
|
| 20 |
+
return "", chat_history
|
| 21 |
+
|
| 22 |
+
try:
|
| 23 |
+
# Initialize agent with error handling
|
| 24 |
+
llm = ChatGroq(groq_api_key=api_key, model_name="Llama3-8b-8192")
|
| 25 |
+
tools = [search, arxiv, wiki]
|
| 26 |
+
|
| 27 |
+
agent = initialize_agent(
|
| 28 |
+
tools,
|
| 29 |
+
llm,
|
| 30 |
+
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
|
| 31 |
+
handle_parsing_errors=True
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
# Using the recommended .invoke() method instead of deprecated .run()
|
| 35 |
+
callback = StdOutCallbackHandler()
|
| 36 |
+
response = agent.invoke(
|
| 37 |
+
{"input": message},
|
| 38 |
+
{"callbacks": [callback]}
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
chat_history.append((message, response["output"]))
|
| 42 |
+
|
| 43 |
+
except Exception as e:
|
| 44 |
+
error_msg = f"β Error: {str(e)}"
|
| 45 |
+
if "API key" in str(e):
|
| 46 |
+
error_msg = "π Invalid or missing API key"
|
| 47 |
+
chat_history.append((message, error_msg))
|
| 48 |
+
|
| 49 |
+
return "", chat_history
|
| 50 |
+
|
| 51 |
+
# UI Layout (unchanged)
|
| 52 |
+
with gr.Blocks(title="π LangChain - Chat with Search") as demo:
|
| 53 |
+
gr.Markdown("""
|
| 54 |
+
## π LangChain - Chat with Search
|
| 55 |
+
Chatbot that can search the web, arXiv, and Wikipedia.
|
| 56 |
+
""")
|
| 57 |
+
|
| 58 |
+
with gr.Row():
|
| 59 |
+
with gr.Column(scale=1):
|
| 60 |
+
api_key = gr.Textbox(
|
| 61 |
+
label="Groq API Key",
|
| 62 |
+
type="password",
|
| 63 |
+
placeholder="Enter your API key"
|
| 64 |
+
)
|
| 65 |
+
with gr.Column(scale=4):
|
| 66 |
+
chatbot = gr.Chatbot(height=500)
|
| 67 |
+
msg = gr.Textbox(
|
| 68 |
+
placeholder="What is machine learning?",
|
| 69 |
+
label="Your Question"
|
| 70 |
+
)
|
| 71 |
+
clear = gr.ClearButton([msg, chatbot])
|
| 72 |
+
|
| 73 |
+
msg.submit(
|
| 74 |
+
respond,
|
| 75 |
+
[msg, chatbot, api_key],
|
| 76 |
+
[msg, chatbot]
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
if __name__ == "__main__":
|
| 80 |
+
demo.launch(debug=True) # Added debug mode for development
|