Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -32,7 +32,7 @@ class URLProcessor:
|
|
32 |
self.session = requests.Session()
|
33 |
self.timeout = 10 # seconds
|
34 |
self.session.headers.update({
|
35 |
-
'User
|
36 |
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
|
37 |
'Accept-Language': 'en-US,en;q=0.5',
|
38 |
'Accept-Encoding': 'gzip, deflate, br',
|
@@ -98,7 +98,7 @@ class URLProcessor:
|
|
98 |
return None
|
99 |
|
100 |
direct_url = f"https://drive.google.com/uc?export=download&id={file_id.group(1)}"
|
101 |
-
response = self.session.get(direct_url, timeout
|
102 |
response.raise_for_status()
|
103 |
|
104 |
return {
|
@@ -223,6 +223,32 @@ class FileProcessor:
|
|
223 |
logger.error(f"File processing error: {e}")
|
224 |
return []
|
225 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
226 |
def create_interface():
|
227 |
"""Create a comprehensive Gradio interface with advanced features"""
|
228 |
|
@@ -255,11 +281,27 @@ def create_interface():
|
|
255 |
placeholder="Paste your text here..."
|
256 |
)
|
257 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
258 |
process_btn = gr.Button("Process Input", variant="primary")
|
259 |
|
260 |
output_text = gr.Textbox(label="Processing Results", interactive=False)
|
261 |
output_file = gr.File(label="Processed Output")
|
262 |
|
|
|
|
|
|
|
263 |
def process_all_inputs(urls, file, text):
|
264 |
"""Process all input types with progress tracking"""
|
265 |
try:
|
@@ -314,18 +356,39 @@ def create_interface():
|
|
314 |
except Exception as e:
|
315 |
logger.error(f"Processing error: {e}")
|
316 |
return None, f"Error: {str(e)}"
|
317 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
318 |
process_btn.click(
|
319 |
process_all_inputs,
|
320 |
inputs=[url_input, file_input, text_input],
|
321 |
outputs=[output_file, output_text]
|
322 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
323 |
|
324 |
gr.Markdown("""
|
325 |
### Usage Guidelines
|
326 |
- **URL Processing**: Enter valid HTTP/HTTPS URLs
|
327 |
- **File Input**: Upload text files or ZIP archives
|
328 |
- **Text Input**: Direct text processing
|
|
|
329 |
- Advanced cleaning and validation included
|
330 |
""")
|
331 |
|
|
|
32 |
self.session = requests.Session()
|
33 |
self.timeout = 10 # seconds
|
34 |
self.session.headers.update({
|
35 |
+
'User-Agent': UserAgent().random,
|
36 |
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
|
37 |
'Accept-Language': 'en-US,en;q=0.5',
|
38 |
'Accept-Encoding': 'gzip, deflate, br',
|
|
|
98 |
return None
|
99 |
|
100 |
direct_url = f"https://drive.google.com/uc?export=download&id={file_id.group(1)}"
|
101 |
+
response = self.session.get(direct_url, timeout=self.timeout)
|
102 |
response.raise_for_status()
|
103 |
|
104 |
return {
|
|
|
223 |
logger.error(f"File processing error: {e}")
|
224 |
return []
|
225 |
|
226 |
+
class Chatbot:
|
227 |
+
"""Simple chatbot that uses provided JSON data for responses."""
|
228 |
+
|
229 |
+
def __init__(self):
|
230 |
+
self.data = None
|
231 |
+
|
232 |
+
def load_data(self, json_data: str):
|
233 |
+
"""Load JSON data into the chatbot."""
|
234 |
+
try:
|
235 |
+
self.data = json.loads(json_data)
|
236 |
+
return "Data loaded successfully!"
|
237 |
+
except json.JSONDecodeError:
|
238 |
+
return "Invalid JSON data. Please check your input."
|
239 |
+
|
240 |
+
def chat(self, user_input: str) -> str:
|
241 |
+
"""Generate a response based on user input and loaded data."""
|
242 |
+
if not self.data:
|
243 |
+
return "No data loaded. Please load your JSON data first."
|
244 |
+
|
245 |
+
# Simple keyword-based response logic
|
246 |
+
for key, value in self.data.items():
|
247 |
+
if key.lower() in user_input.lower():
|
248 |
+
return f"{key}: {value}"
|
249 |
+
|
250 |
+
return "I don't have information on that. Please ask about something else."
|
251 |
+
|
252 |
def create_interface():
|
253 |
"""Create a comprehensive Gradio interface with advanced features"""
|
254 |
|
|
|
281 |
placeholder="Paste your text here..."
|
282 |
)
|
283 |
|
284 |
+
with gr.Tab("Chat"):
|
285 |
+
json_input = gr.Textbox(
|
286 |
+
label="Load JSON Data",
|
287 |
+
placeholder="Paste your JSON data here...",
|
288 |
+
lines=5
|
289 |
+
)
|
290 |
+
load_btn = gr.Button("Load Data", variant="primary")
|
291 |
+
chat_input = gr.Textbox(
|
292 |
+
label="Chat with your data",
|
293 |
+
placeholder="Type your question here..."
|
294 |
+
)
|
295 |
+
chat_output = gr.Textbox(label="Chatbot Response", interactive=False)
|
296 |
+
|
297 |
process_btn = gr.Button("Process Input", variant="primary")
|
298 |
|
299 |
output_text = gr.Textbox(label="Processing Results", interactive=False)
|
300 |
output_file = gr.File(label="Processed Output")
|
301 |
|
302 |
+
# Initialize chatbot
|
303 |
+
chatbot = Chatbot()
|
304 |
+
|
305 |
def process_all_inputs(urls, file, text):
|
306 |
"""Process all input types with progress tracking"""
|
307 |
try:
|
|
|
356 |
except Exception as e:
|
357 |
logger.error(f"Processing error: {e}")
|
358 |
return None, f"Error: {str(e)}"
|
359 |
+
|
360 |
+
def load_chat_data(json_data):
|
361 |
+
"""Load JSON data into the chatbot."""
|
362 |
+
return chatbot.load_data(json_data)
|
363 |
+
|
364 |
+
def chat_with_data(user_input):
|
365 |
+
"""Chat with the loaded data."""
|
366 |
+
return chatbot.chat(user_input)
|
367 |
+
|
368 |
process_btn.click(
|
369 |
process_all_inputs,
|
370 |
inputs=[url_input, file_input, text_input],
|
371 |
outputs=[output_file, output_text]
|
372 |
)
|
373 |
+
|
374 |
+
load_btn.click(
|
375 |
+
load_chat_data,
|
376 |
+
inputs=json_input,
|
377 |
+
outputs=chat_output
|
378 |
+
)
|
379 |
+
|
380 |
+
chat_input.submit(
|
381 |
+
chat_with_data,
|
382 |
+
inputs=chat_input,
|
383 |
+
outputs=chat_output
|
384 |
+
)
|
385 |
|
386 |
gr.Markdown("""
|
387 |
### Usage Guidelines
|
388 |
- **URL Processing**: Enter valid HTTP/HTTPS URLs
|
389 |
- **File Input**: Upload text files or ZIP archives
|
390 |
- **Text Input**: Direct text processing
|
391 |
+
- **Chat**: Load JSON data and ask questions about it
|
392 |
- Advanced cleaning and validation included
|
393 |
""")
|
394 |
|