Create app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354import gradio as grfrom langchain.agents import Agentfrom langchain.llms import HuggingFaceHubfrom langchain.vectorstores import FAISSfrom langchain.embeddings import HuggingFaceEmbeddings
# Initialize the LLM from Hugging Face Hubllm = HuggingFaceHub(repo_id="gpt2")
# Initialize embeddingsembeddings = 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 rolesclass 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 agentsresearch_agent = ResearchAgent()summary_agent = SummaryAgent()qa_agent = QAAgent()
# Function to handle the interaction with the agentsdef 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 interfaceinterface = 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()app.pyAdded+54