Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from langchain.agents import Agent
|
3 |
+
from langchain.llms import HuggingFaceHub
|
4 |
+
from langchain.vectorstores import FAISS
|
5 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
6 |
+
|
7 |
+
# Initialize the LLM from Hugging Face Hub
|
8 |
+
llm = HuggingFaceHub(repo_id="gpt2")
|
9 |
+
|
10 |
+
# Initialize embeddings
|
11 |
+
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
12 |
+
|
13 |
+
# Initialize the vector database (FAISS)
|
14 |
+
vectorstore = FAISS(embeddings.embed_query, embeddings.embed_documents)
|
15 |
+
|
16 |
+
# Define the agents with distinct roles
|
17 |
+
class ResearchAgent(Agent):
|
18 |
+
def run(self, query):
|
19 |
+
return llm(query + " Please provide a detailed explanation.")
|
20 |
+
|
21 |
+
class SummaryAgent(Agent):
|
22 |
+
def run(self, query):
|
23 |
+
return llm(query + " Summarize the information briefly.")
|
24 |
+
|
25 |
+
class QAAgent(Agent):
|
26 |
+
def run(self, query):
|
27 |
+
return llm(query + " Answer the following question: " + query)
|
28 |
+
|
29 |
+
# Create instances of the agents
|
30 |
+
research_agent = ResearchAgent()
|
31 |
+
summary_agent = SummaryAgent()
|
32 |
+
qa_agent = QAAgent()
|
33 |
+
|
34 |
+
# Function to handle the interaction with the agents
|
35 |
+
def agent_interaction(query, agent_type):
|
36 |
+
if agent_type == "Research":
|
37 |
+
return research_agent.run(query)
|
38 |
+
elif agent_type == "Summary":
|
39 |
+
return summary_agent.run(query)
|
40 |
+
elif agent_type == "Q&A":
|
41 |
+
return qa_agent.run(query)
|
42 |
+
|
43 |
+
# Create a Gradio interface
|
44 |
+
interface = gr.Interface(
|
45 |
+
fn=agent_interaction,
|
46 |
+
inputs=[
|
47 |
+
gr.inputs.Textbox(lines=2, placeholder="Enter your query here..."),
|
48 |
+
gr.inputs.Radio(["Research", "Summary", "Q&A"], label="Agent Type")
|
49 |
+
],
|
50 |
+
outputs="text"
|
51 |
+
)
|
52 |
+
|
53 |
+
if __name__ == "__main__":
|
54 |
+
interface.launch()
|