Spaces:
Running
Running
File size: 9,396 Bytes
7f65bf9 e6ed86a 7f65bf9 c3b1d58 7f65bf9 e6ed86a 7f65bf9 c3b1d58 7f65bf9 554b5c7 7f65bf9 c3b1d58 7f65bf9 e08a69a c3b1d58 7f65bf9 e6ed86a 7f65bf9 e6ed86a 554b5c7 e6ed86a 7f65bf9 e6ed86a 7f65bf9 e6ed86a 7f65bf9 e6ed86a 7f65bf9 e6ed86a 7f65bf9 e6ed86a 7f65bf9 e6ed86a 7f65bf9 e6ed86a c3b1d58 e6ed86a e08a69a e6ed86a e08a69a e6ed86a e08a69a e6ed86a e08a69a 554b5c7 e6ed86a 7f65bf9 e6ed86a 7f65bf9 e6ed86a 7f65bf9 e6ed86a 554b5c7 e6ed86a 554b5c7 e6ed86a 554b5c7 e6ed86a 7f65bf9 e6ed86a 554b5c7 e6ed86a 554b5c7 e6ed86a 7f65bf9 e6ed86a 554b5c7 e6ed86a 554b5c7 e6ed86a 7f65bf9 e6ed86a 554b5c7 7f65bf9 17fdb3b 7f65bf9 e6ed86a 7f65bf9 17fdb3b 554b5c7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
"""
Advanced URL & Text Processing Suite - Main Application
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
A sophisticated Gradio interface with URL processing, file manipulation, QR operations,
and advanced data chat capabilities.
"""
import gradio as gr
import logging
import json
import os
import sys
import zipfile
import pandas as pd
import numpy as np
from datetime import datetime
from pathlib import Path
from typing import Dict, List, Optional, Union, Any, Tuple
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s.%(msecs)03d [%(levelname)s] %(name)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
)
logger = logging.getLogger(__name__)
# Modern UI Configuration
THEME = gr.themes.Soft(
primary_hue="indigo",
secondary_hue="blue",
neutral_hue="slate",
spacing_size=gr.themes.sizes.spacing_md,
radius_size=gr.themes.sizes.radius_md,
text_size=gr.themes.sizes.text_md,
)
class DataChatProcessor:
def __init__(self):
self.trained_data = {}
self.current_dataset = None
def process_zip_file(self, file_obj, mode):
try:
if not file_obj:
return "Please upload a ZIP file", []
# Extract ZIP contents
with zipfile.ZipFile(file_obj.name, 'r') as zip_ref:
temp_dir = Path('temp_data')
temp_dir.mkdir(exist_ok=True)
zip_ref.extractall(temp_dir)
# Process based on mode
if mode == "TrainedOnData":
return self._train_on_data(temp_dir)
else: # TalkAboutData
return self._analyze_data(temp_dir)
except Exception as e:
logger.error(f"Error processing ZIP file: {e}")
return f"Error: {str(e)}", []
def _train_on_data(self, data_dir):
try:
datasets = []
for file in data_dir.glob('**/*.csv'):
df = pd.read_csv(file)
datasets.append({
'name': file.name,
'data': df,
'summary': {
'rows': len(df),
'columns': len(df.columns),
'dtypes': df.dtypes.astype(str).to_dict()
}
})
self.trained_data = {
'datasets': datasets,
'timestamp': datetime.now().isoformat()
}
summary = f"Trained on {len(datasets)} datasets"
messages = [
{"role": "assistant", "content": "Training completed successfully."},
{"role": "assistant", "content": summary}
]
return summary, messages
except Exception as e:
logger.error(f"Error training on data: {e}")
return f"Error during training: {str(e)}", []
def _analyze_data(self, data_dir):
try:
analyses = []
for file in data_dir.glob('**/*.csv'):
df = pd.read_csv(file)
analyses.append({
'file': file.name,
'shape': df.shape,
'dtypes': df.dtypes.astype(str).to_dict()
})
self.current_dataset = {
'analyses': analyses,
'timestamp': datetime.now().isoformat()
}
summary = f"Analyzed {len(analyses)} files"
messages = [
{"role": "assistant", "content": "Analysis completed successfully."},
{"role": "assistant", "content": summary}
]
return summary, messages
except Exception as e:
logger.error(f"Error analyzing data: {e}")
return f"Error during analysis: {str(e)}", []
def chat(self, message, history, mode):
if not message:
return "", history
history.append({"role": "user", "content": message})
try:
if mode == "TrainedOnData":
if not self.trained_data:
response = "Please upload and train on data first."
else:
response = self._generate_trained_response(message)
else:
if not self.current_dataset:
response = "Please upload data for analysis first."
else:
response = self._generate_analysis_response(message)
history.append({"role": "assistant", "content": response})
return "", history
except Exception as e:
logger.error(f"Error in chat: {e}")
history.append({"role": "assistant", "content": f"Error: {str(e)}"})
return "", history
def _generate_trained_response(self, message):
datasets = self.trained_data['datasets']
if "how many" in message.lower():
return f"There are {len(datasets)} datasets."
if "summary" in message.lower():
summaries = []
for ds in datasets:
summaries.append(
f"Dataset '{ds['name']}': {ds['summary']['rows']} rows, "
f"{ds['summary']['columns']} columns"
)
return "\n".join(summaries)
return "I can help you analyze the trained datasets. Ask about number of datasets or summaries."
def _generate_analysis_response(self, message):
analyses = self.current_dataset['analyses']
if "how many" in message.lower():
return f"There are {len(analyses)} files."
if "summary" in message.lower():
summaries = []
for analysis in analyses:
summaries.append(
f"File '{analysis['file']}': {analysis['shape'][0]} rows, "
f"{analysis['shape'][1]} columns"
)
return "\n".join(summaries)
return "I can help you explore the current dataset. Ask about file count or summaries."
def create_interface():
data_chat = DataChatProcessor()
with gr.Blocks(theme=THEME) as interface:
gr.Markdown(
"""
# π Advanced Data Processing & Analysis Suite
Enterprise-grade toolkit for data processing, analysis, and interactive chat capabilities.
"""
)
with gr.Tab("π¬ DataChat"):
with gr.Row():
# Left column for file upload and mode selection
with gr.Column(scale=1):
data_file = gr.File(
label="Upload ZIP File",
file_types=[".zip"]
)
mode = gr.Radio(
choices=["TrainedOnData", "TalkAboutData"],
value="TrainedOnData",
label="Chat Mode"
)
process_btn = gr.Button("Process Data", variant="primary")
status_output = gr.Textbox(
label="Status",
interactive=False
)
# Right column for chat interface
with gr.Column(scale=2):
chatbot = gr.Chatbot(
label="Chat History",
height=400,
show_label=True,
type="messages" # Specify OpenAI-style message format
)
msg = gr.Textbox(
label="Your Message",
placeholder="Ask questions about your data...",
lines=2
)
with gr.Row():
submit_btn = gr.Button("Send", variant="primary")
clear_btn = gr.Button("Clear Chat", variant="secondary")
# Event handlers
process_btn.click(
fn=data_chat.process_zip_file,
inputs=[data_file, mode],
outputs=[status_output, chatbot]
)
submit_btn.click(
fn=data_chat.chat,
inputs=[msg, chatbot, mode],
outputs=[msg, chatbot]
)
msg.submit(
fn=data_chat.chat,
inputs=[msg, chatbot, mode],
outputs=[msg, chatbot]
)
clear_btn.click(
fn=lambda: ([], "Chat cleared"),
outputs=[chatbot, status_output]
)
return interface
def main():
try:
interface = create_interface()
if interface:
interface.launch(
server_name="0.0.0.0",
server_port=8000
)
else:
logger.error("Failed to create interface")
sys.exit(1)
except Exception as e:
logger.error(f"Application startup error: {e}", exc_info=True)
sys.exit(1)
if __name__ == "__main__":
main() |