agents / app.py
Merlintxu's picture
Create app.py
9b7ca42 verified
raw
history blame
1.66 kB
import gradio as gr
from langchain.agents import Agent
from langchain.llms import HuggingFaceHub
from langchain.vectorstores import FAISS
from langchain.embeddings import HuggingFaceEmbeddings
# Initialize the LLM from Hugging Face Hub
llm = HuggingFaceHub(repo_id="gpt2")
# Initialize embeddings
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
# Initialize the vector database (FAISS)
vectorstore = FAISS(embeddings.embed_query, embeddings.embed_documents)
# Define the agents with distinct roles
class ResearchAgent(Agent):
def run(self, query):
return llm(query + " Please provide a detailed explanation.")
class SummaryAgent(Agent):
def run(self, query):
return llm(query + " Summarize the information briefly.")
class QAAgent(Agent):
def run(self, query):
return llm(query + " Answer the following question: " + query)
# Create instances of the agents
research_agent = ResearchAgent()
summary_agent = SummaryAgent()
qa_agent = QAAgent()
# Function to handle the interaction with the agents
def agent_interaction(query, agent_type):
if agent_type == "Research":
return research_agent.run(query)
elif agent_type == "Summary":
return summary_agent.run(query)
elif agent_type == "Q&A":
return qa_agent.run(query)
# Create a Gradio interface
interface = gr.Interface(
fn=agent_interaction,
inputs=[
gr.inputs.Textbox(lines=2, placeholder="Enter your query here..."),
gr.inputs.Radio(["Research", "Summary", "Q&A"], label="Agent Type")
],
outputs="text"
)
if __name__ == "__main__":
interface.launch()