Update app.py
Browse files
app.py
CHANGED
@@ -1,15 +1,15 @@
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
from sqlalchemy import text
|
4 |
-
from smolagents import
|
5 |
import pandas as pd
|
6 |
from io import StringIO
|
|
|
7 |
from database import (
|
8 |
engine,
|
9 |
create_dynamic_table,
|
10 |
clear_database,
|
11 |
-
insert_rows_into_table
|
12 |
-
get_table_schema
|
13 |
)
|
14 |
|
15 |
# Initialize the AI agent
|
@@ -47,7 +47,6 @@ def process_txt_file(file_path):
|
|
47 |
with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
|
48 |
content = f.read()
|
49 |
|
50 |
-
# Structure detection with enhanced prompts
|
51 |
structure_prompt = f"""
|
52 |
Convert this text into valid CSV format:
|
53 |
{content}
|
@@ -62,7 +61,6 @@ def process_txt_file(file_path):
|
|
62 |
"""
|
63 |
csv_output = agent.run(structure_prompt)
|
64 |
|
65 |
-
# Robust CSV parsing
|
66 |
try:
|
67 |
df = pd.read_csv(
|
68 |
StringIO(csv_output),
|
@@ -76,7 +74,6 @@ def process_txt_file(file_path):
|
|
76 |
if df.empty or len(df.columns) == 0:
|
77 |
return False, "No structured data found", pd.DataFrame()
|
78 |
|
79 |
-
# Database operations
|
80 |
clear_database()
|
81 |
table = create_dynamic_table(df)
|
82 |
insert_rows_into_table(df.to_dict('records'), table)
|
@@ -89,20 +86,34 @@ def process_txt_file(file_path):
|
|
89 |
def handle_upload(file_obj):
|
90 |
"""Handle file upload and processing"""
|
91 |
if file_obj is None:
|
92 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
|
94 |
success, message, df = process_txt_file(file_obj)
|
95 |
if success:
|
96 |
-
column_info = {col: {'type': 'text'} for col in df.columns}
|
97 |
schema = "\n".join([f"- {col} (text)" for col in df.columns])
|
98 |
-
return
|
99 |
message,
|
100 |
df,
|
101 |
f"### Detected Schema:\n```\n{schema}\n```",
|
102 |
gr.update(visible=False),
|
|
|
103 |
gr.update(visible=True)
|
104 |
-
|
105 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
106 |
|
107 |
def query_analysis(user_query: str) -> str:
|
108 |
"""Handle natural language queries about the data"""
|
@@ -130,6 +141,16 @@ def query_analysis(user_query: str) -> str:
|
|
130 |
except Exception as e:
|
131 |
return f"Query error: {str(e)}"
|
132 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
133 |
# Gradio interface setup
|
134 |
with gr.Blocks() as demo:
|
135 |
with gr.Group() as upload_group:
|
@@ -147,7 +168,9 @@ with gr.Blocks() as demo:
|
|
147 |
with gr.Group(visible=False) as query_group:
|
148 |
with gr.Row():
|
149 |
with gr.Column(scale=1):
|
150 |
-
|
|
|
|
|
151 |
query_output = gr.Markdown(label="Analysis Results")
|
152 |
with gr.Column(scale=2):
|
153 |
gr.Markdown("### Extracted Data Preview")
|
@@ -155,6 +178,10 @@ with gr.Blocks() as demo:
|
|
155 |
label="Structured Data",
|
156 |
interactive=False
|
157 |
)
|
|
|
|
|
|
|
|
|
158 |
schema_display = gr.Markdown()
|
159 |
refresh_btn = gr.Button("Refresh View")
|
160 |
|
@@ -162,9 +189,15 @@ with gr.Blocks() as demo:
|
|
162 |
file_input.upload(
|
163 |
fn=handle_upload,
|
164 |
inputs=file_input,
|
165 |
-
outputs=[status, data_table, schema_display, upload_group, query_group]
|
166 |
)
|
167 |
|
|
|
|
|
|
|
|
|
|
|
|
|
168 |
user_input.submit(
|
169 |
fn=query_analysis,
|
170 |
inputs=user_input,
|
@@ -175,6 +208,11 @@ with gr.Blocks() as demo:
|
|
175 |
fn=lambda: (get_data_table().head(10), "Schema refreshed"),
|
176 |
outputs=[data_table, schema_display]
|
177 |
)
|
|
|
|
|
|
|
|
|
|
|
178 |
|
179 |
if __name__ == "__main__":
|
180 |
demo.launch(
|
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
from sqlalchemy import text
|
4 |
+
from smolagents import CodeAgent, HfApiModel
|
5 |
import pandas as pd
|
6 |
from io import StringIO
|
7 |
+
import tempfile
|
8 |
from database import (
|
9 |
engine,
|
10 |
create_dynamic_table,
|
11 |
clear_database,
|
12 |
+
insert_rows_into_table
|
|
|
13 |
)
|
14 |
|
15 |
# Initialize the AI agent
|
|
|
47 |
with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
|
48 |
content = f.read()
|
49 |
|
|
|
50 |
structure_prompt = f"""
|
51 |
Convert this text into valid CSV format:
|
52 |
{content}
|
|
|
61 |
"""
|
62 |
csv_output = agent.run(structure_prompt)
|
63 |
|
|
|
64 |
try:
|
65 |
df = pd.read_csv(
|
66 |
StringIO(csv_output),
|
|
|
74 |
if df.empty or len(df.columns) == 0:
|
75 |
return False, "No structured data found", pd.DataFrame()
|
76 |
|
|
|
77 |
clear_database()
|
78 |
table = create_dynamic_table(df)
|
79 |
insert_rows_into_table(df.to_dict('records'), table)
|
|
|
86 |
def handle_upload(file_obj):
|
87 |
"""Handle file upload and processing"""
|
88 |
if file_obj is None:
|
89 |
+
return [
|
90 |
+
"Please upload a text file.",
|
91 |
+
None,
|
92 |
+
"No schema",
|
93 |
+
gr.update(visible=True),
|
94 |
+
gr.update(visible=False),
|
95 |
+
gr.update(visible=False)
|
96 |
+
]
|
97 |
|
98 |
success, message, df = process_txt_file(file_obj)
|
99 |
if success:
|
|
|
100 |
schema = "\n".join([f"- {col} (text)" for col in df.columns])
|
101 |
+
return [
|
102 |
message,
|
103 |
df,
|
104 |
f"### Detected Schema:\n```\n{schema}\n```",
|
105 |
gr.update(visible=False),
|
106 |
+
gr.update(visible=True),
|
107 |
gr.update(visible=True)
|
108 |
+
]
|
109 |
+
return [
|
110 |
+
message,
|
111 |
+
None,
|
112 |
+
"No schema",
|
113 |
+
gr.update(visible=True),
|
114 |
+
gr.update(visible=False),
|
115 |
+
gr.update(visible=False)
|
116 |
+
]
|
117 |
|
118 |
def query_analysis(user_query: str) -> str:
|
119 |
"""Handle natural language queries about the data"""
|
|
|
141 |
except Exception as e:
|
142 |
return f"Query error: {str(e)}"
|
143 |
|
144 |
+
def download_csv():
|
145 |
+
"""Generate CSV file for download"""
|
146 |
+
df = get_data_table()
|
147 |
+
if not df.empty:
|
148 |
+
temp_dir = tempfile.gettempdir()
|
149 |
+
file_path = os.path.join(temp_dir, "processed_data.csv")
|
150 |
+
df.to_csv(file_path, index=False)
|
151 |
+
return file_path
|
152 |
+
return None
|
153 |
+
|
154 |
# Gradio interface setup
|
155 |
with gr.Blocks() as demo:
|
156 |
with gr.Group() as upload_group:
|
|
|
168 |
with gr.Group(visible=False) as query_group:
|
169 |
with gr.Row():
|
170 |
with gr.Column(scale=1):
|
171 |
+
with gr.Row():
|
172 |
+
user_input = gr.Textbox(label="Ask about the data", scale=4)
|
173 |
+
submit_btn = gr.Button("Submit", scale=1)
|
174 |
query_output = gr.Markdown(label="Analysis Results")
|
175 |
with gr.Column(scale=2):
|
176 |
gr.Markdown("### Extracted Data Preview")
|
|
|
178 |
label="Structured Data",
|
179 |
interactive=False
|
180 |
)
|
181 |
+
download_btn = gr.DownloadButton(
|
182 |
+
"Download as CSV",
|
183 |
+
visible=False
|
184 |
+
)
|
185 |
schema_display = gr.Markdown()
|
186 |
refresh_btn = gr.Button("Refresh View")
|
187 |
|
|
|
189 |
file_input.upload(
|
190 |
fn=handle_upload,
|
191 |
inputs=file_input,
|
192 |
+
outputs=[status, data_table, schema_display, upload_group, query_group, download_btn]
|
193 |
)
|
194 |
|
195 |
+
submit_btn.click(
|
196 |
+
fn=query_analysis,
|
197 |
+
inputs=user_input,
|
198 |
+
outputs=query_output
|
199 |
+
)
|
200 |
+
|
201 |
user_input.submit(
|
202 |
fn=query_analysis,
|
203 |
inputs=user_input,
|
|
|
208 |
fn=lambda: (get_data_table().head(10), "Schema refreshed"),
|
209 |
outputs=[data_table, schema_display]
|
210 |
)
|
211 |
+
|
212 |
+
download_btn.click(
|
213 |
+
fn=download_csv,
|
214 |
+
outputs=download_btn
|
215 |
+
)
|
216 |
|
217 |
if __name__ == "__main__":
|
218 |
demo.launch(
|