VishwaTharunChalla commited on
Commit
b15be4b
·
1 Parent(s): 522a7ff

added all the files

Browse files
.env ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ NEBIUS_API_KEY = "eyJhbGciOiJIUzI1NiIsImtpZCI6IlV6SXJWd1h0dnprLVRvdzlLZWstc0M1akptWXBvX1VaVkxUZlpnMDRlOFUiLCJ0eXAiOiJKV1QifQ.eyJzdWIiOiJnb29nbGUtb2F1dGgyfDExMDgzMjI4NDU1OTcyMzc4OTEyMyIsInNjb3BlIjoib3BlbmlkIG9mZmxpbmVfYWNjZXNzIiwiaXNzIjoiYXBpX2tleV9pc3N1ZXIiLCJhdWQiOlsiaHR0cHM6Ly9uZWJpdXMtaW5mZXJlbmNlLmV1LmF1dGgwLmNvbS9hcGkvdjIvIl0sImV4cCI6MTkwNjgyMzA1OCwidXVpZCI6IjNmOTliMDMxLWRkNjUtNGYyMS1iZDE0LWViMTQyOTU0Nzg1MCIsIm5hbWUiOiJoYWNrYXRob24iLCJleHBpcmVzX2F0IjoiMjAzMC0wNi0wNFQxNzowNDoxOCswMDAwIn0.mJ1D7AFDdpRnsPDTk14xR0KSP_ND2cUA8DUuR3GevEk"
2
+ QDRANT_API_KEY = "EvKLIIodeepwz9P8WGsIAGnYgPVKmoIce9oaoxT65lA9G9MCa6keyQ"
3
+ QDRANT_URL = "https://2d9a7822-188b-4f81-ae14-b1c0fd4fbc6f.us-east4-0.gcp.cloud.qdrant.io:6333/"
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ *.db filter=lfs diff=lfs merge=lfs -text
.gitignore ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ hackathon_venv
2
+ *__pycache__
agno_kb.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+ from pathlib import Path
4
+ from dotenv import load_dotenv
5
+ from agno.embedder.openai import OpenAIEmbedder
6
+ from agno.knowledge.pdf import PDFKnowledgeBase, PDFReader
7
+ from agno.vectordb.qdrant import Qdrant
8
+ from agno.document.chunking.fixed import FixedSizeChunking
9
+
10
+ # Load environment variables
11
+ load_dotenv()
12
+
13
+ QDRANT_URL = os.getenv("QDRANT_URL")
14
+ QDRANT_API_KEY = os.getenv("QDRANT_API_KEY")
15
+
16
+ # embeddings = OpenAIEmbedder(
17
+ # id="text-embedding-3-large",
18
+ # dimensions=3072,
19
+ # api_key=os.getenv("OPENAI_API_KEY")
20
+ # )
21
+
22
+ embeddings = OpenAIEmbedder(
23
+ id="BAAI/bge-en-icl",
24
+ dimensions=4096,
25
+ api_key=os.getenv("NEBIUS_API_KEY"),
26
+ base_url="https://api.studio.nebius.com/v1/"
27
+ )
28
+
29
+
30
+ class AgnoKnowledgeBase:
31
+ def __init__(self, query: str, user_id: str, thread_id: str, agno_kb_config: dict,
32
+ chunk_size: int = 1000, num_documents: int = 6):
33
+ self.query = query
34
+ self.user_id = user_id
35
+ self.thread_id = thread_id
36
+ self.agno_kb_config = agno_kb_config
37
+ self.chunk_size = chunk_size
38
+ self.num_documents = num_documents
39
+
40
+ def setup_knowledge_base(self):
41
+ print(self.agno_kb_config)
42
+ agno_kb_config = self.agno_kb_config['knowledge_base']
43
+ input_data = agno_kb_config.get("input_data", {})
44
+ sources = input_data.get("source", [])
45
+ recreate = agno_kb_config.get("recreate", False)
46
+ collection_name = agno_kb_config.get("collection_name")
47
+ chunk_size = agno_kb_config.get("chunk_size")
48
+ overlap = agno_kb_config.get("overlap")
49
+ num_documents = agno_kb_config.get("num_documents")
50
+ chunking_strategy_type = agno_kb_config.get("chunking_strategy", "fixed")
51
+
52
+ if chunking_strategy_type == "fixed":
53
+ chunking_strategy = FixedSizeChunking(chunk_size=chunk_size, overlap=overlap)
54
+ else:
55
+ raise ValueError(f"Unsupported chunking strategy: {chunking_strategy_type}")
56
+
57
+ vector_db = Qdrant(
58
+ collection=collection_name,
59
+ embedder=embeddings,
60
+ url=QDRANT_URL,
61
+ api_key=QDRANT_API_KEY
62
+ )
63
+
64
+ for source in sources:
65
+ paths = source.get("path", [])
66
+ for path in paths:
67
+ print(f"Loading PDF into Qdrant: {path}")
68
+ knowledge_base = PDFKnowledgeBase(
69
+ path=path,
70
+ vector_db=vector_db,
71
+ reader=PDFReader(),
72
+ chunking_strategy=chunking_strategy,
73
+ num_documents=num_documents
74
+ )
75
+ knowledge_base.load(recreate=recreate)
76
+
77
+ return PDFKnowledgeBase(
78
+ path=None,
79
+ vector_db=vector_db,
80
+ reader=PDFReader(),
81
+ chunking_strategy=chunking_strategy,
82
+ num_documents=num_documents
83
+ )
dynamic_agent.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import traceback
3
+ from dotenv import load_dotenv
4
+ from agno.agent import Agent
5
+ from agno.storage.agent.sqlite import SqliteAgentStorage
6
+ from agno.memory.agent import AgentMemory
7
+ from agno.models.nebius import Nebius
8
+
9
+ load_dotenv()
10
+ NEBIUS_API_KEY = os.getenv("NEBIUS_API_KEY")
11
+ DB_NAME = "hackathon.db"
12
+
13
+ storage = SqliteAgentStorage(table_name="hackathon_storage", db_file=DB_NAME)
14
+ memory = AgentMemory()
15
+
16
+ class AgentFactory:
17
+ def __init__(self, user_id, thread_id, agent_config: dict, knowledge_base):
18
+ self.user_id = user_id
19
+ self.thread_id = thread_id
20
+ self.agent_config = agent_config
21
+ self.knowledge_base = knowledge_base
22
+
23
+ async def routing_agent(self):
24
+ try:
25
+ routing_agent = Agent(
26
+ model=Nebius(
27
+ id="meta-llama/Meta-Llama-3.1-405B-Instruct",
28
+ temperature=0,
29
+ api_key=NEBIUS_API_KEY,
30
+ base_url="https://api.studio.nebius.com/v1/"
31
+ ),
32
+ name="Routing Agent",
33
+ description="You are a helpful routing assistant. This agent is responsible for routing the user's message to the appropriate agent. Based on the question it has to provide response. If question relates to 'plot', 'chart', 'graph', 'visualize', 'visualization', 'visual','bar chart', 'line chart', 'pie chart', 'scatter plot', 'histogram', 'heatmap', 'dashboard', 'show me', 'display', 'draw', 'create chart','generate plot', 'make graph', 'data visualization', 'analytics','trends', 'comparison chart', 'infographic'. If the question relates to the visualization like above key points then respond with 'visualization' else respond 'normal'.",
34
+ instructions=[
35
+ "You should only respond with 'normal' or 'visualization'.",
36
+ "DO NOT add any delimiter between the response and the word 'normal' or 'visualization'.",
37
+ "Your response should be one word accordingly.",
38
+ ],
39
+ show_tool_calls=True,
40
+ markdown=True,
41
+ debug_mode=True
42
+ )
43
+ return routing_agent
44
+ except Exception as e:
45
+ print("Error creating routing agent:", traceback.format_exc())
46
+ raise e
47
+
48
+ async def normal_and_reasoning_agent(self, tools, model_name) -> Agent:
49
+ try:
50
+ agent = Agent(
51
+ model=Nebius(
52
+ id=model_name, #meta-llama/Meta-Llama-3.1-405B-Instruct #Qwen/Qwen3-235B-A22B #Qwen/Qwen3-30B-A3B
53
+ temperature=0,
54
+ api_key=NEBIUS_API_KEY,
55
+ base_url="https://api.studio.nebius.com/v1/"
56
+ ),
57
+ name=self.agent_config["name"],
58
+ description=self.agent_config["description"],
59
+ instructions=self.agent_config["instructions"],
60
+ tools=tools,
61
+ show_tool_calls=True,
62
+ markdown=True,
63
+ debug_mode=True,
64
+ knowledge=self.knowledge_base,
65
+ search_knowledge=True,
66
+ storage=storage,
67
+ memory=memory,
68
+ user_id=self.user_id,
69
+ add_history_to_messages=True,
70
+ session_id=self.thread_id,
71
+ num_history_responses=10
72
+ )
73
+ return agent
74
+ except Exception as e:
75
+ print("Error creating agent:", traceback.format_exc())
76
+ raise e
flipkart_mobiles.db ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:98d3d1aaac70b4be7da0ca79f57b4e0863d004a50710a092ffa8a12a6ff62b9a
3
+ size 159744
mcp_tools.py ADDED
@@ -0,0 +1,203 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import sqlite3
3
+ import traceback
4
+ import os
5
+ import re
6
+ import uuid
7
+ from agno.tools import tool
8
+ import pandas as pd
9
+ from matplotlib import pyplot as plt
10
+ import seaborn as sns
11
+ import plotly.express as px
12
+
13
+ # --- DB Functions ---
14
+ def init_product_db():
15
+ conn = sqlite3.connect("flipkart_mobiles.db")
16
+ cursor = conn.cursor()
17
+ cursor.execute('''
18
+ CREATE TABLE IF NOT EXISTS mobiles (
19
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
20
+ brand TEXT,
21
+ color TEXT,
22
+ model TEXT,
23
+ memory TEXT,
24
+ storage TEXT,
25
+ rating REAL,
26
+ selling_price REAL,
27
+ original_price REAL
28
+ )
29
+ ''')
30
+ conn.commit()
31
+ conn.close()
32
+
33
+ def read_products():
34
+ conn = sqlite3.connect("flipkart_mobiles.db")
35
+ cursor = conn.cursor()
36
+ cursor.execute("SELECT * FROM mobiles")
37
+ rows = cursor.fetchall()
38
+ conn.close()
39
+ return rows
40
+
41
+ # --- Tool Wrappers ---
42
+ DB_PATH = "flipkart_mobiles.db"
43
+ TABLE_NAME = "mobiles"
44
+
45
+ @tool(show_result=True, stop_after_tool_call=True)
46
+ def get_columns_info_from_database(columns: str = "*"):
47
+ """
48
+ Database Schema: brand, color, model, memory, storage, rating, selling_price, original_price
49
+ Table: mobiles
50
+
51
+ Query the 'mobiles' table selecting specified columns dynamically.
52
+
53
+ Input:
54
+ - columns: a comma-separated string of column names to select, e.g. "brand, model, rating"
55
+ If "*", selects all columns.
56
+
57
+ Returns:
58
+ - Formatted string of rows with selected columns.
59
+ """
60
+ if columns.strip() != "*":
61
+ if not re.fullmatch(r"[a-zA-Z0-9_,\s]+", columns):
62
+ return "Invalid columns format."
63
+
64
+ conn = sqlite3.connect(DB_PATH)
65
+ cursor = conn.cursor()
66
+
67
+ # Build query string dynamically
68
+ query = f"SELECT {columns} FROM {TABLE_NAME}"
69
+
70
+ try:
71
+ cursor.execute(query)
72
+ rows = cursor.fetchall()
73
+
74
+ # Get column names from cursor description
75
+ col_names = [desc[0] for desc in cursor.description]
76
+
77
+ output_lines = []
78
+ for row in rows:
79
+ row_dict = dict(zip(col_names, row))
80
+ formatted_row = ", ".join(f"{col}: {row_dict[col]}" for col in col_names)
81
+ output_lines.append(formatted_row)
82
+
83
+ return "\n".join(output_lines) if output_lines else "No rows found."
84
+ except Exception as e:
85
+ return f"Query error: {str(e)}"
86
+ finally:
87
+ conn.close()
88
+
89
+ @tool(show_result=True, stop_after_tool_call=True)
90
+ def generate_python_code(python_code: str) -> str:
91
+ """
92
+ You are a Python data scientist. Take the table and columns information from the chat history or agent memory.
93
+
94
+ Your task is to generate a valid Python script from the following response.
95
+ This table and columns information can be in raw English or structured format from the chat history or agent memory like:
96
+ - user: task - description
97
+ - tabular strings
98
+ - JSON-like text
99
+ - general descriptive statistics
100
+
101
+ You must:
102
+ 1. Convert the data into a pandas DataFrame (use variable name `df`)
103
+ 2. Select an appropriate chart (bar chart, pie chart, line chart, etc.) based on the user's query
104
+ 3. Use matplotlib, seaborn, or plotly to plot. Any one of it to create the chart or graph or plot
105
+ 4. Save the chart using the variable `image_path` to a PNG file
106
+ 5. Return only the Python code — no comments, no markdown
107
+
108
+ ### Rules:
109
+ - Do not use `plt.show()` or any GUI renderer
110
+ - Use clear axis labels and title
111
+ - Save the figure using `plt.savefig(image_path)`
112
+ - `df` must be used for all data manipulations
113
+ - You must generate the full Python code block
114
+ - execute that Python code and return the path to the saved image folder.
115
+ - Create an image into the "plots" folder.
116
+
117
+ Example code:
118
+ ```python
119
+ import pandas as pd
120
+ import matplotlib.pyplot as plt
121
+
122
+ data = [
123
+ {"id": 1, "name": "Alice", "task": "NLP"},
124
+ {"id": 2, "name": "Bob", "task": "Vision"},
125
+ {"id": 3, "name": "Alice", "task": "NLP"}
126
+ ]
127
+
128
+ df = pd.DataFrame(data)
129
+ task_counts = df["task"].value_counts()
130
+
131
+ plt.figure(figsize=(6, 4))
132
+ task_counts.plot(kind="bar", color="skyblue")
133
+ plt.xlabel("Task")
134
+ plt.ylabel("Count")
135
+ plt.title("Task Distribution")
136
+ plt.savefig(image_path)
137
+ ```
138
+ """
139
+ return python_code
140
+
141
+ @tool(show_result=True, stop_after_tool_call=True)
142
+ def visualization_tool(python_code: str) -> str:
143
+ """ This function is for taking the python code as input from chat history or agent memory and cleaning it accordingly so that it can be executed, then executing it and returning the image path.
144
+ """
145
+ try:
146
+ cleaned_code = re.sub(r"^```(?:python)?|```$", "", python_code.strip(), flags=re.MULTILINE)
147
+ image_path = f"plots/{uuid.uuid4().hex}.png"
148
+ os.makedirs("plots", exist_ok=True)
149
+ exec_context = {
150
+ "pd": pd,
151
+ "plt": plt,
152
+ "sns": sns,
153
+ "px": px,
154
+ "image_path": image_path
155
+ }
156
+ exec(cleaned_code, exec_context)
157
+ return image_path
158
+ except Exception:
159
+ return f"Error executing visualization code:\n{traceback.format_exc()}"
160
+
161
+ # --- Init DB ---
162
+ init_product_db()
163
+
164
+ # --- Define Toolkit ---
165
+ toolkit = [
166
+ get_columns_info_from_database,
167
+ generate_python_code,
168
+ visualization_tool
169
+ ]
170
+
171
+ # --- Gradio UI ---
172
+ tabbed = gr.TabbedInterface(
173
+ interface_list=[
174
+ gr.Interface(
175
+ fn=get_columns_info_from_database.entrypoint,
176
+ inputs=[
177
+ gr.Textbox(label="Columns (comma separated, or * for all)", value="*")
178
+ ],
179
+ outputs=gr.Textbox(label="Query Result"),
180
+ title="Query Products"
181
+ ),
182
+ gr.Interface(
183
+ fn=generate_python_code.entrypoint,
184
+ inputs=[
185
+ gr.Textbox(label="Python code for Visualization", lines=10)
186
+ ],
187
+ outputs=gr.Textbox(label="Python Code for Visualization"),
188
+ title="Python Code Generation"
189
+ ),
190
+ gr.Interface(
191
+ fn=visualization_tool.entrypoint,
192
+ inputs=[
193
+ gr.Textbox(label="Visualization", lines=10)
194
+ ],
195
+ outputs=gr.Textbox(label="Saved Image Path"),
196
+ title="Auto Visualization"
197
+ )
198
+ ],
199
+ tab_names=["Query Products", "Python Code Generation", "Auto Visualization"]
200
+ )
201
+
202
+ # tabbed.launch(mcp_server=True)
203
+ tabbed.launch(server_port=7863, mcp_server=True)
plots/temp ADDED
File without changes
requirements.txt ADDED
Binary file (530 Bytes). View file
 
session_files/0c994316/0c994316-4985-4588-9557-3425ede97b70_Updated_Resume_VT.pdf ADDED
Binary file (87.7 kB). View file