Rajesh Betkiker commited on
Commit
a7c9a13
Β·
1 Parent(s): 0321eee

Added app.py

Browse files
Files changed (1) hide show
  1. app.py +151 -0
app.py ADDED
@@ -0,0 +1,151 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ This file is the main file for the hackathon.
3
+ It contains the Gradio interface for the hackathon as a MCP server.
4
+ It exposes the following tools:
5
+ - search_knowledge_base_for_context
6
+ - research_write_review_topic
7
+
8
+ """
9
+ from dotenv import load_dotenv
10
+ load_dotenv() # Load environment variables from .env file
11
+
12
+ import logging
13
+
14
+ # Configure logging to write to a file instead of stdout/stderr
15
+ # This avoids interference with the MCP communication channel
16
+ logging.basicConfig(
17
+ filename='hackathon-mcp.log', # Log to a file instead of stdout/stderr
18
+ level=logging.INFO,
19
+ format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
20
+ )
21
+ logger = logging.getLogger(__name__)
22
+
23
+ import gradio as gr
24
+ from tools.rag_tools import search_groundx_for_rag_context
25
+ from tools.multi_agent_workflow_for_research import run_research_workflow
26
+
27
+ def search_knowledge_base_for_context(query: str) -> str:
28
+ """
29
+ Searches and retrieves relevant context from a knowledge base (GroundX),
30
+ based on the user's query.
31
+
32
+ Example queries:
33
+ - "What are the main features of fuel system of SU-35."
34
+ - "What are the combat potential of SU-35."
35
+
36
+ Args:
37
+ query: The search query supplied by the user.
38
+
39
+ Returns:
40
+ str: Relevant text content that can be used by the LLM to answer the query.
41
+ """
42
+ logger.info(f"Searching document for RAG context {query}")
43
+ response = search_groundx_for_rag_context(query)
44
+ logger.info(f"RAG Response: {response}")
45
+ return response
46
+
47
+ def research_write_review_topic(query: str) -> str:
48
+ """
49
+ Helps with writing a report with research, writing, and review on any topic.
50
+ Returns a reviewed topic.
51
+
52
+ The query is a string that contains the topic to be researched and reviewed.
53
+
54
+ Example queries:
55
+ - "Write me a report on the history of the internet."
56
+ - "Write me a report on origin of the universe."
57
+ - "Write me a report on the impact of climate change on polar bears."
58
+ - "Write me a report on the benefits of meditation."
59
+ - "Write me a report on the future of artificial intelligence."
60
+ - "Write me a report on the effects of social media on mental health."
61
+
62
+ Args:
63
+ query (str): The query to research, write and review .
64
+
65
+ Returns:
66
+ str: A nicely formatted string.
67
+ """
68
+ try:
69
+ logger.info(f"Researching the topic: {query}")
70
+ result = run_research_workflow(query)
71
+ return result or "Research completed, but no content was generated."
72
+ except Exception as e:
73
+ return f"Error: {e}"
74
+
75
+ with gr.Blocks() as server_info:
76
+ gr.Markdown("""
77
+ # MCP powered RAG and Research
78
+
79
+ I present to you a MCP powered RAG and Research.
80
+
81
+ RAG Tool uses GroundX service to fetch the knowledge base. The knowledge base is a document that contains information about the SU-35 aircraft, including its features, capabilities, and specifications.
82
+ Please check [this PDF](https://airgroup2000.com/gallery/albums/userpics/32438/SU-35_TM_eng.pdf) to formulate queries on Sukhoi.
83
+
84
+ The Research Tool is implemented using multi-agent workflow using LlamaIndex (ResearchAgent, WriteAgent, and ReviewAgent).
85
+
86
+ ## Available Tools
87
+
88
+ ### search_knowledge_base_for_context
89
+ - **Description**: Searches and retrieves relevant context from a knowledge base based on the user's query.
90
+ - **Example Queries**:
91
+ - "What are the main features of fuel system of SU-35."
92
+ - "What are the combat potential of SU-35."
93
+
94
+ ### research_write_review_topic
95
+ - **Description**: Helps with writing a report with research, writing, and review on any topic.
96
+ - **Example Queries**:
97
+ - "Write me a report on the history of the internet."
98
+ - "Write me a report on origin of the universe."
99
+ - "Write me a report on the impact of climate change on polar bears."
100
+
101
+ ## How to Use
102
+ - Use the MCP RAG Tool tab above to query the knowledge base.
103
+ - Use the Research Tool tab above to write report on any topic.
104
+
105
+ ## Demo Link
106
+ [Link to Demo on Youtube](https://www.youtube.com/mcp-rag-research)
107
+ """)
108
+
109
+ mcp_rag_tool = gr.Interface(
110
+ fn=search_knowledge_base_for_context,
111
+ inputs=["text"],
112
+ outputs=[gr.Textbox(label="Knowledge Base", max_lines=10)],
113
+ title="MCP RAG Tool",
114
+ description="Searches and retrieves relevant context from a knowledge base"
115
+ )
116
+
117
+ research_tool = gr.Interface(
118
+ fn=research_write_review_topic,
119
+ inputs=["text"],
120
+ outputs=[gr.Textbox(label="Reviewed Topic", max_lines=10)],
121
+ title="Research Tool",
122
+ description="Helps with report writing with research, writing, and review agents on any topic. ",
123
+ concurrency_limit=10
124
+ )
125
+
126
+ named_interfaces = {
127
+ "Project Information": server_info,
128
+ "RAG - Tool": mcp_rag_tool,
129
+ "Research a Topic - Tool": research_tool
130
+ }
131
+
132
+ # Tab names and interfaces
133
+ tab_names = list(named_interfaces.keys())
134
+ interface_list = list(named_interfaces.values())
135
+
136
+ mcp_server = gr.TabbedInterface(
137
+ interface_list,
138
+ tab_names=tab_names,
139
+ title="πŸ‘ MCP powered RAG and Research 🌍"
140
+ )
141
+
142
+ # Launch the MCP Server
143
+ if __name__ == "__main__":
144
+ mcp_server.queue(default_concurrency_limit=10)
145
+ mcp_server.launch(
146
+ server_name="0.0.0.0",
147
+ server_port=7860,
148
+ share=False,
149
+ debug=False,
150
+ mcp_server=True
151
+ )