Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from langchain.agents import load_tools, initialize_agent
|
| 3 |
+
from langchain.agents import AgentType
|
| 4 |
+
from langchain.chat_models import ChatOpenAI
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
# Set your OpenAI API key (ensure to store it securely in Hugging Face Spaces environment variables)
|
| 8 |
+
#os.environ["OPENAI_API_KEY"] = "your_openai_api_key"
|
| 9 |
+
import warnings
|
| 10 |
+
warnings.filterwarnings("ignore", message=".*TqdmWarning.*")
|
| 11 |
+
from dotenv import load_dotenv
|
| 12 |
+
|
| 13 |
+
_ = load_dotenv()
|
| 14 |
+
|
| 15 |
+
# Define the LLM model
|
| 16 |
+
llm_model = "gpt-3.5-turbo"
|
| 17 |
+
llm = ChatOpenAI(temperature=0, model=llm_model)
|
| 18 |
+
|
| 19 |
+
# Load tools
|
| 20 |
+
tools = load_tools(["llm-math", "wikipedia"], llm=llm)
|
| 21 |
+
|
| 22 |
+
# Initialize agent
|
| 23 |
+
agent = initialize_agent(
|
| 24 |
+
tools,
|
| 25 |
+
llm,
|
| 26 |
+
agent=AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION,
|
| 27 |
+
handle_parsing_errors=True,
|
| 28 |
+
verbose=True
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
def chatbot(query):
|
| 32 |
+
"""Handles user query and returns agent response."""
|
| 33 |
+
try:
|
| 34 |
+
response = agent.run(query)
|
| 35 |
+
return response
|
| 36 |
+
except Exception as e:
|
| 37 |
+
return str(e)
|
| 38 |
+
|
| 39 |
+
# Create Gradio interface
|
| 40 |
+
#demo = gr.Interface(
|
| 41 |
+
#fn=chatbot,
|
| 42 |
+
#inputs=gr.Textbox(placeholder="Ask me anything..."),
|
| 43 |
+
#outputs="text",
|
| 44 |
+
#title="LangChain Agent Chatbot",
|
| 45 |
+
#description="An interactive chatbot powered by LangChain, OpenAI, and external tools."
|
| 46 |
+
#)
|
| 47 |
+
|
| 48 |
+
demo = gr.Interface(
|
| 49 |
+
fn=chatbot,
|
| 50 |
+
inputs=gr.Textbox(label="Your Question", placeholder="Ask me anything..."),
|
| 51 |
+
outputs=gr.Textbox(label="Response"),
|
| 52 |
+
title="LangChain AI Chatbot",
|
| 53 |
+
description="A smart AI chatbot powered by OpenAI and LangChain.",
|
| 54 |
+
theme="compact"
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
# Launch the app
|
| 59 |
+
demo.launch()
|