Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,51 +1,62 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from langchain_community.agent_toolkits.load_tools import load_tools
|
| 3 |
-
from langchain.agents import initialize_agent
|
| 4 |
-
from
|
| 5 |
-
from langchain_openai import ChatOpenAI # Updated import
|
| 6 |
-
import os
|
| 7 |
-
|
| 8 |
-
# Set your OpenAI API key (ensure to store it securely in Hugging Face Spaces environment variables)
|
| 9 |
-
# os.environ["OPENAI_API_KEY"] = "your_openai_api_key"
|
| 10 |
import warnings
|
| 11 |
-
warnings.filterwarnings("ignore", message=".*TqdmWarning.*")
|
| 12 |
-
from dotenv import load_dotenv
|
| 13 |
-
|
| 14 |
-
_ = load_dotenv()
|
| 15 |
|
| 16 |
-
|
| 17 |
-
llm_model = "gpt-3.5-turbo"
|
| 18 |
-
llm = ChatOpenAI(temperature=0, model=llm_model, openai_api_key=os.getenv('OPEN_API_KEY')) # Ensure to pass the API key
|
| 19 |
|
| 20 |
-
# Load tools
|
| 21 |
-
tools = load_tools(["llm-math", "wikipedia"], llm=llm)
|
| 22 |
|
| 23 |
-
#
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
agent
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
)
|
| 31 |
|
| 32 |
-
def chatbot(query):
|
| 33 |
-
"""Handles user query and returns agent response."""
|
| 34 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
response = agent.run(query)
|
| 36 |
return response
|
|
|
|
| 37 |
except Exception as e:
|
| 38 |
-
return str(e)
|
|
|
|
| 39 |
|
| 40 |
-
#
|
|
|
|
|
|
|
| 41 |
demo = gr.Interface(
|
| 42 |
fn=chatbot,
|
| 43 |
-
inputs=
|
|
|
|
|
|
|
|
|
|
| 44 |
outputs=gr.Textbox(label="Response"),
|
| 45 |
title="LangChain AI Chatbot",
|
| 46 |
-
description="
|
| 47 |
theme="compact"
|
| 48 |
)
|
| 49 |
|
| 50 |
-
#
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from langchain_community.agent_toolkits.load_tools import load_tools
|
| 3 |
+
from langchain.agents import initialize_agent, AgentType
|
| 4 |
+
from langchain_openai import ChatOpenAI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
import warnings
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
warnings.filterwarnings("ignore", message=".*TqdmWarning.*")
|
|
|
|
|
|
|
| 8 |
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
# ----------------------
|
| 11 |
+
# Chatbot function
|
| 12 |
+
# ----------------------
|
| 13 |
+
def chatbot(api_key: str, query: str) -> str:
|
| 14 |
+
"""Handles user query and returns agent response using the provided API key."""
|
| 15 |
+
if not api_key.strip():
|
| 16 |
+
return "⚠️ Please enter a valid OpenAI API key."
|
|
|
|
| 17 |
|
|
|
|
|
|
|
| 18 |
try:
|
| 19 |
+
# Initialize model with user-provided key
|
| 20 |
+
llm_model = "gpt-3.5-turbo"
|
| 21 |
+
llm = ChatOpenAI(temperature=0, model=llm_model, openai_api_key=api_key)
|
| 22 |
+
|
| 23 |
+
# Load tools
|
| 24 |
+
tools = load_tools(["llm-math", "wikipedia"], llm=llm)
|
| 25 |
+
|
| 26 |
+
# Initialize agent
|
| 27 |
+
agent = initialize_agent(
|
| 28 |
+
tools,
|
| 29 |
+
llm,
|
| 30 |
+
agent=AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION,
|
| 31 |
+
handle_parsing_errors=True,
|
| 32 |
+
verbose=True
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
# Run the query
|
| 36 |
response = agent.run(query)
|
| 37 |
return response
|
| 38 |
+
|
| 39 |
except Exception as e:
|
| 40 |
+
return f"❌ Error: {str(e)}"
|
| 41 |
+
|
| 42 |
|
| 43 |
+
# ----------------------
|
| 44 |
+
# Gradio Interface
|
| 45 |
+
# ----------------------
|
| 46 |
demo = gr.Interface(
|
| 47 |
fn=chatbot,
|
| 48 |
+
inputs=[
|
| 49 |
+
gr.Textbox(label="🔑 OpenAI API Key", type="password", placeholder="Paste your API key here..."),
|
| 50 |
+
gr.Textbox(label="Your Question", placeholder="Ask me anything...")
|
| 51 |
+
],
|
| 52 |
outputs=gr.Textbox(label="Response"),
|
| 53 |
title="LangChain AI Chatbot",
|
| 54 |
+
description="Enter your OpenAI API key and ask me anything! Powered by OpenAI and LangChain.",
|
| 55 |
theme="compact"
|
| 56 |
)
|
| 57 |
|
| 58 |
+
# ----------------------
|
| 59 |
+
# Launch
|
| 60 |
+
# ----------------------
|
| 61 |
+
if __name__ == "__main__":
|
| 62 |
+
demo.launch(share=True)
|