Update app.py
Browse files
app.py
CHANGED
@@ -1,28 +1,28 @@
|
|
1 |
-
import json
|
2 |
-
import logging
|
3 |
-
import os
|
4 |
-
from datetime import datetime
|
5 |
-
from typing import Dict, List, Optional, Any, Tuple
|
6 |
-
from dataclasses import dataclass, field
|
7 |
-
from pathlib import Path
|
8 |
|
9 |
# Third-party imports
|
10 |
-
import gradio as gr
|
11 |
-
from openai import OpenAI
|
12 |
|
13 |
# Configure logging
|
14 |
-
logging.basicConfig(
|
15 |
-
level=logging.INFO,
|
16 |
-
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
17 |
-
handlers=[
|
18 |
-
logging.StreamHandler(),
|
19 |
-
logging.FileHandler('app.log')
|
20 |
-
]
|
21 |
-
)
|
22 |
-
logger = logging.getLogger(__name__)
|
23 |
|
24 |
# System prompt for the AI assistant
|
25 |
-
SYSTEM_PROMPT = """
|
26 |
You are an Information Extraction Assistant, designed to help extract and organize
|
27 |
important information from conversations in a natural and engaging way.
|
28 |
|
@@ -33,105 +33,106 @@ Core Capabilities:
|
|
33 |
- Structured data organization with context preservation
|
34 |
|
35 |
Please maintain a friendly and professional tone while ensuring accurate information extraction.
|
36 |
-
"""
|
37 |
-
|
38 |
-
@dataclass
|
39 |
-
class ExtractedInfo:
|
40 |
-
"""Structure for storing extracted information."""
|
41 |
-
text: str
|
42 |
-
category: str
|
43 |
-
confidence: float
|
44 |
-
timestamp: datetime = field(default_factory=datetime.now)
|
45 |
-
metadata: Dict[str, Any] = field(default_factory=dict)
|
46 |
-
|
47 |
-
@dataclass
|
48 |
-
class ConversationState:
|
49 |
-
"""Tracks the state and progress of the conversation."""
|
50 |
-
extracted_items: List[ExtractedInfo] = field(default_factory=list)
|
51 |
-
categories_covered: List[str] = field(default_factory=list)
|
52 |
-
current_focus: Optional[str] = None
|
53 |
-
completion_percentage: float = 0.0
|
54 |
-
last_error: Optional[str] = None
|
55 |
-
last_update: datetime = field(default_factory=datetime.now)
|
56 |
-
|
57 |
-
def add_extracted_info(self, info: ExtractedInfo) -> None:
|
58 |
-
"""Add new extracted information and update state."""
|
59 |
-
self.extracted_items.append(info)
|
60 |
-
if info.category not in self.categories_covered:
|
61 |
-
self.categories_covered.append(info.category)
|
62 |
-
self.last_update = datetime.now()
|
63 |
-
|
64 |
-
class InformationExtractor:
|
65 |
-
"""Core class for handling information extraction from conversations."""
|
66 |
|
67 |
-
def __init__(self):
|
68 |
-
self.conversation_history: List[Dict[str, str]] = []
|
69 |
-
self.state = ConversationState()
|
70 |
-
self.client: Optional[OpenAI] = None
|
71 |
-
self.extraction_categories = [
|
72 |
-
"personal_info",
|
73 |
-
"education",
|
74 |
-
"work_experience",
|
75 |
-
"skills",
|
76 |
-
"achievements"
|
77 |
-
]
|
78 |
|
79 |
-
def _validate_api_key(self, api_key: str) -> bool:
|
80 |
-
"""Validate OpenAI API key format."""
|
81 |
-
if not api_key.strip():
|
82 |
-
raise ValueError("API key cannot be empty")
|
83 |
-
if not api_key.startswith('sk-'):
|
84 |
-
raise ValueError("Invalid API key format")
|
85 |
-
return True
|
86 |
|
87 |
-
def _initialize_client(self, api_key: str) -> None:
|
88 |
-
"""Initialize OpenAI client with error handling."""
|
89 |
-
try:
|
90 |
-
if self._validate_api_key(api_key):
|
91 |
-
self.client = OpenAI(api_key=api_key)
|
92 |
-
except Exception as e:
|
93 |
-
logger.error(f"Error initializing OpenAI client: {str(e)}")
|
94 |
-
raise
|
95 |
-
|
96 |
-
def _add_to_history(self, role: str, content: str) -> None:
|
97 |
-
"""Add a message to conversation history with timestamp."""
|
98 |
-
self.conversation_history.append({
|
99 |
-
"role": role,
|
100 |
-
"content": content,
|
101 |
-
"timestamp": datetime.now().isoformat()
|
102 |
-
})
|
103 |
-
|
104 |
-
def _get_ai_response(self, retries: int = 3) -> str:
|
105 |
-
"""Get response from OpenAI with retry mechanism."""
|
106 |
-
if not self.client:
|
107 |
-
raise ValueError("OpenAI client not initialized")
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
"
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
|
|
122 |
|
123 |
-
return response.choices[0].message.content
|
124 |
|
125 |
-
except Exception as e:
|
126 |
-
logger.warning(f"Attempt {attempt + 1} failed: {str(e)}")
|
127 |
-
if attempt == retries - 1:
|
128 |
-
raise Exception(f"Failed after {retries} attempts: {str(e)}")
|
129 |
-
continue
|
130 |
-
|
131 |
-
def _extract_information(self, text: str) -> List[ExtractedInfo]:
|
132 |
-
"""Extract structured information from text."""
|
133 |
-
try:
|
134 |
-
extraction_prompt = f"""
|
135 |
Analyze the following text and extract relevant information.
|
136 |
Categories to consider: {', '.join(self.extraction_categories)}
|
137 |
|
@@ -154,489 +155,492 @@ for attempt in range(retries): #83
|
|
154 |
}}
|
155 |
|
156 |
Text to analyze: {text}
|
157 |
-
"""
|
158 |
|
159 |
-
response = self.client.chat.completions.create(
|
160 |
-
model="gpt-
|
161 |
-
messages=[
|
162 |
-
{"role": "system", "content": SYSTEM_PROMPT},
|
163 |
-
{"role": "user", "content": extraction_prompt}
|
164 |
-
],
|
165 |
-
temperature=0.3
|
166 |
-
)
|
167 |
|
168 |
-
# Parse response and create ExtractedInfo objects
|
169 |
-
analysis = json.loads(response.choices[0].message.content)
|
170 |
-
extracted_items = []
|
171 |
|
172 |
-
for item in analysis.get("extracted_items", []):
|
173 |
-
extracted_info = ExtractedInfo(
|
174 |
-
text=item["text"],
|
175 |
-
category=item["category"],
|
176 |
-
confidence=item["confidence"],
|
177 |
-
metadata=item.get("metadata", {})
|
178 |
-
)
|
179 |
-
extracted_items.append(extracted_info)
|
180 |
|
181 |
-
return extracted_items
|
182 |
|
183 |
-
except json.JSONDecodeError as e:
|
184 |
-
logger.error(f"Error parsing extraction response: {str(e)}")
|
185 |
-
return []
|
186 |
-
except Exception as e:
|
187 |
-
logger.error(f"Error during information extraction: {str(e)}")
|
188 |
-
return []
|
189 |
-
|
190 |
-
def _update_completion_status(self) -> None:
|
191 |
-
"""Update completion status based on extracted information."""
|
192 |
-
total_categories = len(self.extraction_categories)
|
193 |
-
covered_categories = len(self.state.categories_covered)
|
194 |
|
195 |
-
# Calculate base completion percentage
|
196 |
-
base_completion = (covered_categories / total_categories) * 100
|
197 |
|
198 |
-
# Adjust based on confidence levels
|
199 |
-
if self.state.extracted_items:
|
200 |
-
avg_confidence = sum(item.confidence for item in self.state.extracted_items) / len(self.state.extracted_items)
|
201 |
-
adjusted_completion = base_completion * avg_confidence
|
202 |
-
else:
|
203 |
-
adjusted_completion = 0.0
|
204 |
|
205 |
-
self.state.completion_percentage = min(adjusted_completion, 100.0)
|
206 |
|
207 |
-
def process_message(self, message: str, api_key: str) -> Dict[str, Any]:
|
208 |
-
"""Process a user message and extract information."""
|
209 |
-
try:
|
210 |
-
# Initialize client if needed
|
211 |
-
if not self.client:
|
212 |
-
self._initialize_client(api_key)
|
213 |
|
214 |
-
# Add user message to history
|
215 |
-
self._add_to_history("user", message)
|
216 |
|
217 |
-
# Get AI response
|
218 |
-
ai_response = self._get_ai_response()
|
219 |
-
self._add_to_history("assistant", ai_response)
|
220 |
|
221 |
-
# Extract information from the entire conversation
|
222 |
-
new_information = self._extract_information(message + "\n" + ai_response)
|
223 |
|
224 |
-
# Update state with new information
|
225 |
-
for info in new_information:
|
226 |
-
self.state.add_extracted_info(info)
|
227 |
|
228 |
-
# Update completion status
|
229 |
-
self._update_completion_status()
|
230 |
|
231 |
-
return {
|
232 |
-
"response": ai_response,
|
233 |
-
"extracted_info": [
|
234 |
-
{
|
235 |
-
"text": info.text,
|
236 |
-
"category": info.category,
|
237 |
-
"confidence": info.confidence
|
238 |
-
} for info in new_information
|
239 |
-
],
|
240 |
-
"completion_status": {
|
241 |
-
"percentage": self.state.completion_percentage,
|
242 |
-
"categories_covered": self.state.categories_covered,
|
243 |
-
"current_focus": self.state.current_focus
|
244 |
-
}
|
245 |
-
}
|
246 |
|
247 |
-
except Exception as e:
|
248 |
-
error_msg = f"Error processing message: {str(e)}"
|
249 |
-
logger.error(error_msg)
|
250 |
-
self.state.last_error = error_msg
|
251 |
-
return {
|
252 |
-
"error": error_msg,
|
253 |
-
"completion_status": {
|
254 |
-
"percentage": self.state.completion_percentage,
|
255 |
-
"categories_covered": self.state.categories_covered,
|
256 |
-
"current_focus": self.state.current_focus
|
257 |
-
}
|
258 |
-
}
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
"
|
269 |
-
"
|
270 |
-
"
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
"
|
282 |
-
|
283 |
-
"
|
284 |
-
"
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
"
|
289 |
-
"
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
|
|
|
296 |
|
297 |
-
with open(filename, 'w', encoding='utf-8') as f:
|
298 |
-
json.dump(output, f, indent=2, ensure_ascii=False)
|
299 |
-
|
300 |
-
return {
|
301 |
-
"filename": filename,
|
302 |
-
"content": output,
|
303 |
-
"status": "success"
|
304 |
-
}
|
305 |
-
|
306 |
-
except Exception as e:
|
307 |
-
error_msg = f"Error generating output: {str(e)}"
|
308 |
-
logger.error(error_msg)
|
309 |
-
return {
|
310 |
-
"error": error_msg,
|
311 |
-
"status": "error"
|
312 |
-
}
|
313 |
-
|
314 |
-
def create_gradio_interface():
|
315 |
-
"""Create the Gradio interface for information extraction."""
|
316 |
-
extractor = InformationExtractor()
|
317 |
|
318 |
-
# Custom CSS for better styling
|
319 |
-
css = """
|
320 |
-
.container { max-width: 900px; margin: auto; }
|
321 |
-
.message { padding: 1rem; margin: 0.5rem 0; border-radius: 0.5rem; }
|
322 |
-
.info-panel { background: #f5f5f5; padding: 1rem; border-radius: 0.5rem; }
|
323 |
-
.status-badge {
|
324 |
-
display: inline-block;
|
325 |
-
padding: 0.25rem 0.5rem;
|
326 |
-
border-radius: 0.25rem;
|
327 |
-
margin: 0.25rem;
|
328 |
-
background: #e0e0e0;
|
329 |
-
}
|
330 |
-
.extraction-highlight {
|
331 |
-
background: #e8f4f8;
|
332 |
-
border-left: 4px solid #4a90e2;
|
333 |
-
padding: 0.5rem;
|
334 |
-
margin: 0.5rem 0;
|
335 |
-
}
|
336 |
-
"""
|
337 |
-
|
338 |
-
with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
|
339 |
-
gr.Markdown("""
|
340 |
# 🔍 Information Extraction Assistant
|
341 |
|
342 |
Have a natural conversation while we extract and organize important information.
|
343 |
The system will automatically identify and categorize relevant details.
|
344 |
-
""")
|
345 |
-
|
346 |
-
with gr.Row():
|
347 |
-
with gr.Column(scale=2):
|
348 |
-
# API Key input
|
349 |
-
api_key = gr.Textbox(
|
350 |
-
label="OpenAI API Key",
|
351 |
-
type="password",
|
352 |
-
placeholder="Enter your OpenAI API key (sk-...)",
|
353 |
-
show_label=True
|
354 |
-
)
|
355 |
-
|
356 |
-
# Chat interface
|
357 |
-
chatbot = gr.Chatbot(
|
358 |
-
value=[],
|
359 |
-
height=400,
|
360 |
-
type="messages",
|
361 |
-
show_label=False
|
362 |
-
)
|
363 |
-
|
364 |
-
# Message input
|
365 |
-
with gr.Row():
|
366 |
-
msg = gr.Textbox(
|
367 |
-
label="Message",
|
368 |
-
placeholder="Type your message here...",
|
369 |
-
scale=4
|
370 |
-
)
|
371 |
-
submit = gr.Button(
|
372 |
-
"Send",
|
373 |
-
variant="primary",
|
374 |
-
scale=1
|
375 |
-
)
|
376 |
-
|
377 |
-
# Action buttons
|
378 |
-
with gr.Row():
|
379 |
-
clear = gr.Button("Clear Chat", scale=1)
|
380 |
-
generate = gr.Button(
|
381 |
-
"Generate Report",
|
382 |
-
variant="secondary",
|
383 |
-
scale=2
|
384 |
-
)
|
385 |
-
|
386 |
-
|
387 |
-
|
388 |
-
|
|
|
389 |
|
390 |
-
# Progress indicator
|
391 |
-
progress = gr.Slider(
|
392 |
-
label="Completion",
|
393 |
-
minimum=0,
|
394 |
-
maximum=100,
|
395 |
-
value=0,
|
396 |
-
interactive=False
|
397 |
-
)
|
398 |
|
399 |
-
# Categories covered
|
400 |
-
categories_covered = gr.JSON(
|
401 |
-
label="Categories Covered",
|
402 |
-
value={"categories": []}
|
403 |
-
)
|
404 |
|
405 |
-
# Current focus
|
406 |
-
current_focus = gr.Textbox(
|
407 |
-
label="Current Focus",
|
408 |
-
value="Not started",
|
409 |
-
interactive=False
|
410 |
-
)
|
411 |
-
|
412 |
-
# Extraction Results
|
413 |
-
with gr.Tabs() as result_tabs:
|
414 |
-
with gr.Tab("Extracted Information"):
|
415 |
-
extracted_info = gr.JSON(
|
416 |
-
label="Extracted Details",
|
417 |
-
value={}
|
418 |
-
)
|
419 |
|
420 |
-
with gr.Tab("Download"):
|
421 |
-
file_output = gr.File(
|
422 |
-
label="Download Report"
|
423 |
-
)
|
424 |
|
425 |
-
with gr.Tab("Analysis"):
|
426 |
-
analysis_text = gr.Markdown(
|
427 |
-
"Analysis will appear here after processing."
|
428 |
-
)
|
429 |
-
|
430 |
-
# Helper Functions
|
431 |
-
def format_extraction_summary(extracted_items: List[Dict]) -> str:
|
432 |
-
"""Format extracted information for display."""
|
433 |
-
if not extracted_items:
|
434 |
-
return "No information extracted yet."
|
435 |
|
436 |
-
summary = ["### Recently Extracted Information"]
|
437 |
-
for item in extracted_items:
|
438 |
-
summary.append(
|
439 |
-
f"- **{item['category']}** ({item['confidence']*100:.1f}% confidence)\n"
|
440 |
-
f" {item['text']}"
|
441 |
-
)
|
442 |
-
return "\n".join(summary)
|
443 |
-
|
444 |
-
def update_interface_state(state: Dict[str, Any]) -> tuple:
|
445 |
-
"""Update all interface components based on current state."""
|
446 |
-
return (
|
447 |
-
state['completion_status']['percentage'],
|
448 |
-
{"categories": state['completion_status']['categories_covered']},
|
449 |
-
state['completion_status']['current_focus']
|
450 |
-
)
|
451 |
-
|
452 |
-
# Event Handlers
|
453 |
-
def process_message(message: str, history: list, key: str) -> tuple:
|
454 |
-
"""Handle message processing and update interface."""
|
455 |
-
if not message.strip():
|
456 |
-
return history, 0, {}, "Please enter a message"
|
457 |
|
458 |
-
try:
|
459 |
-
# Process message
|
460 |
-
result = extractor.process_message(message, key)
|
461 |
|
462 |
-
if "error" in result:
|
463 |
-
return (
|
464 |
-
history,
|
465 |
-
0,
|
466 |
-
{"categories": []},
|
467 |
-
f"Error: {result['error']}"
|
468 |
-
)
|
469 |
|
470 |
-
# Update chat history
|
471 |
-
history.append({
|
472 |
-
"role": "user",
|
473 |
-
"content": message
|
474 |
-
})
|
475 |
-
history.append({
|
476 |
-
"role": "assistant",
|
477 |
-
"content": result["response"]
|
478 |
-
})
|
479 |
|
480 |
-
# Update status components
|
481 |
-
progress_value = result["completion_status"]["percentage"]
|
482 |
-
categories = {
|
483 |
-
"categories": result["completion_status"]["categories_covered"]
|
484 |
-
}
|
485 |
-
current_focus = result["completion_status"]["current_focus"] or "Processing..."
|
486 |
|
487 |
-
# Update extraction display
|
488 |
-
if result.get("extracted_info"):
|
489 |
-
analysis_text = format_extraction_summary(result["extracted_info"])
|
490 |
-
else:
|
491 |
-
analysis_text = "No new information extracted."
|
492 |
|
493 |
-
return (
|
494 |
-
history,
|
495 |
-
progress_value,
|
496 |
-
categories,
|
497 |
-
current_focus,
|
498 |
-
analysis_text
|
499 |
-
)
|
500 |
|
501 |
-
except Exception as e:
|
502 |
-
logger.error(f"Error in process_message: {str(e)}")
|
503 |
-
return (
|
504 |
-
history,
|
505 |
-
0,
|
506 |
-
{"categories": []},
|
507 |
-
f"Error: {str(e)}",
|
508 |
-
"An error occurred during processing."
|
509 |
-
)
|
510 |
-
|
511 |
-
|
512 |
-
|
513 |
-
|
|
|
514 |
|
515 |
-
if result["status"] == "success":
|
516 |
-
# Update JSON preview
|
517 |
-
content_preview = {
|
518 |
-
"summary": result["content"]["analysis_summary"],
|
519 |
-
"categories": list(result["content"]["extracted_information"].keys()),
|
520 |
-
"total_items": len(result["content"]["extracted_information"])
|
521 |
-
}
|
522 |
|
523 |
-
return (
|
524 |
-
result["filename"],
|
525 |
-
content_preview,
|
526 |
-
"Report generated successfully! 🎉",
|
527 |
-
gr.update(value=format_extraction_summary(
|
528 |
-
[item for items in result["content"]["extracted_information"].values()
|
529 |
-
for item in items]
|
530 |
-
))
|
531 |
-
)
|
532 |
-
else:
|
533 |
-
return (
|
534 |
-
None,
|
535 |
-
{"error": result["error"]},
|
536 |
-
f"Error generating report: {result['error']}",
|
537 |
-
"Failed to generate analysis."
|
538 |
-
)
|
539 |
|
540 |
-
except Exception as e:
|
541 |
-
logger.error(f"Error in generate_report: {str(e)}")
|
542 |
-
return (
|
543 |
-
None,
|
544 |
-
{"error": str(e)},
|
545 |
-
f"Error: {str(e)}",
|
546 |
-
"An error occurred during report generation."
|
547 |
-
)
|
548 |
-
|
549 |
-
def clear_interface() -> tuple:
|
550 |
-
"""Reset all interface components."""
|
551 |
-
# Reset extractor state
|
552 |
-
global extractor
|
553 |
-
extractor = InformationExtractor()
|
554 |
|
555 |
-
return (
|
556 |
-
[],
|
557 |
-
0.0,
|
558 |
-
{"categories": []},
|
559 |
-
"Not started",
|
560 |
-
{},
|
561 |
-
None,
|
562 |
-
"Ready to start new extraction.",
|
563 |
-
gr.update(value="")
|
564 |
-
)
|
565 |
-
|
566 |
-
# Event Bindings
|
567 |
-
msg.submit(
|
568 |
-
process_message,
|
569 |
-
inputs=[msg, chatbot, api_key],
|
570 |
-
outputs=[
|
571 |
-
chatbot,
|
572 |
-
progress,
|
573 |
-
categories_covered,
|
574 |
-
current_focus,
|
575 |
-
analysis_text
|
576 |
-
]
|
577 |
-
).then(
|
578 |
-
lambda: "",
|
579 |
-
None,
|
580 |
-
msg
|
581 |
-
)
|
582 |
-
|
583 |
-
submit.click(
|
584 |
-
process_message,
|
585 |
-
inputs=[msg, chatbot, api_key],
|
586 |
-
outputs=[
|
587 |
-
chatbot,
|
588 |
-
progress,
|
589 |
-
categories_covered,
|
590 |
-
current_focus,
|
591 |
-
analysis_text
|
592 |
-
]
|
593 |
-
).then(
|
594 |
-
lambda: "",
|
595 |
-
None,
|
596 |
-
msg
|
597 |
-
)
|
598 |
-
|
599 |
-
generate.click(
|
600 |
-
generate_report,
|
601 |
-
outputs=[
|
602 |
-
file_output,
|
603 |
-
extracted_info,
|
604 |
-
current_focus,
|
605 |
-
analysis_text
|
606 |
-
]
|
607 |
-
)
|
608 |
-
|
609 |
-
clear.click(
|
610 |
-
clear_interface,
|
611 |
-
outputs=[
|
612 |
-
chatbot,
|
613 |
-
progress,
|
614 |
-
categories_covered,
|
615 |
-
current_focus,
|
616 |
-
extracted_info,
|
617 |
-
file_output,
|
618 |
-
analysis_text,
|
619 |
-
msg
|
620 |
-
]
|
621 |
-
)
|
622 |
-
|
623 |
-
return demo
|
624 |
-
|
625 |
-
if __name__ == "__main__":
|
626 |
-
# Set up logging for the main application
|
627 |
-
logging.basicConfig(
|
628 |
-
level=logging.INFO,
|
629 |
-
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
630 |
-
)
|
631 |
|
632 |
-
try:
|
633 |
-
demo = create_gradio_interface()
|
634 |
-
demo.launch(
|
635 |
-
server_name="0.0.0.0",
|
636 |
-
server_port=7860,
|
637 |
-
share=True,
|
638 |
-
show_api=False
|
639 |
-
)
|
640 |
-
except Exception as e:
|
641 |
-
logger.error(f"Application failed to start: {str(e)}")
|
642 |
-
raise
|
|
|
1 |
+
import json
|
2 |
+
import logging
|
3 |
+
import os
|
4 |
+
from datetime import datetime
|
5 |
+
from typing import Dict, List, Optional, Any, Tuple
|
6 |
+
from dataclasses import dataclass, field
|
7 |
+
from pathlib import Path
|
8 |
|
9 |
# Third-party imports
|
10 |
+
import gradio as gr
|
11 |
+
from openai import OpenAI
|
12 |
|
13 |
# Configure logging
|
14 |
+
logging.basicConfig(
|
15 |
+
level=logging.INFO,
|
16 |
+
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
17 |
+
handlers=[
|
18 |
+
logging.StreamHandler(),
|
19 |
+
logging.FileHandler('app.log')
|
20 |
+
]
|
21 |
+
)
|
22 |
+
logger = logging.getLogger(__name__)
|
23 |
|
24 |
# System prompt for the AI assistant
|
25 |
+
SYSTEM_PROMPT = """
|
26 |
You are an Information Extraction Assistant, designed to help extract and organize
|
27 |
important information from conversations in a natural and engaging way.
|
28 |
|
|
|
33 |
- Structured data organization with context preservation
|
34 |
|
35 |
Please maintain a friendly and professional tone while ensuring accurate information extraction.
|
36 |
+
"""
|
37 |
+
|
38 |
+
@dataclass
|
39 |
+
class ExtractedInfo:
|
40 |
+
"""Structure for storing extracted information."""
|
41 |
+
text: str
|
42 |
+
category: str
|
43 |
+
confidence: float
|
44 |
+
timestamp: datetime = field(default_factory=datetime.now)
|
45 |
+
metadata: Dict[str, Any] = field(default_factory=dict)
|
46 |
+
|
47 |
+
@dataclass
|
48 |
+
class ConversationState:
|
49 |
+
"""Tracks the state and progress of the conversation."""
|
50 |
+
extracted_items: List[ExtractedInfo] = field(default_factory=list)
|
51 |
+
categories_covered: List[str] = field(default_factory=list)
|
52 |
+
current_focus: Optional[str] = None
|
53 |
+
completion_percentage: float = 0.0
|
54 |
+
last_error: Optional[str] = None
|
55 |
+
last_update: datetime = field(default_factory=datetime.now)
|
56 |
+
|
57 |
+
def add_extracted_info(self, info: ExtractedInfo) -> None:
|
58 |
+
"""Add new extracted information and update state."""
|
59 |
+
self.extracted_items.append(info)
|
60 |
+
if info.category not in self.categories_covered:
|
61 |
+
self.categories_covered.append(info.category)
|
62 |
+
self.last_update = datetime.now()
|
63 |
+
|
64 |
+
class InformationExtractor:
|
65 |
+
"""Core class for handling information extraction from conversations."""
|
66 |
|
67 |
+
def __init__(self):
|
68 |
+
self.conversation_history: List[Dict[str, str]] = []
|
69 |
+
self.state = ConversationState()
|
70 |
+
self.client: Optional[OpenAI] = None
|
71 |
+
self.extraction_categories = [
|
72 |
+
"personal_info",
|
73 |
+
"education",
|
74 |
+
"work_experience",
|
75 |
+
"skills",
|
76 |
+
"achievements"
|
77 |
+
]
|
78 |
|
79 |
+
def _validate_api_key(self, api_key: str) -> bool:
|
80 |
+
"""Validate OpenAI API key format."""
|
81 |
+
if not api_key.strip():
|
82 |
+
raise ValueError("API key cannot be empty")
|
83 |
+
if not api_key.startswith('sk-'):
|
84 |
+
raise ValueError("Invalid API key format")
|
85 |
+
return True
|
86 |
|
87 |
+
def _initialize_client(self, api_key: str) -> None:
|
88 |
+
"""Initialize OpenAI client with error handling."""
|
89 |
+
try:
|
90 |
+
if self._validate_api_key(api_key):
|
91 |
+
self.client = OpenAI(api_key=api_key)
|
92 |
+
except Exception as e:
|
93 |
+
logger.error(f"Error initializing OpenAI client: {str(e)}")
|
94 |
+
raise
|
95 |
+
|
96 |
+
def _add_to_history(self, role: str, content: str) -> None:
|
97 |
+
"""Add a message to conversation history with timestamp."""
|
98 |
+
self.conversation_history.append({
|
99 |
+
"role": role,
|
100 |
+
"content": content,
|
101 |
+
"timestamp": datetime.now().isoformat()
|
102 |
+
})
|
103 |
+
|
104 |
+
def _get_ai_response(self, retries: int = 3) -> str:
|
105 |
+
"""Get response from OpenAI with retry mechanism."""
|
106 |
+
if not self.client:
|
107 |
+
raise ValueError("OpenAI client not initialized")
|
108 |
+
|
109 |
+
for attempt in range(retries):
|
110 |
+
try:
|
111 |
+
response = self.client.chat.completions.create(
|
112 |
+
model="gpt-4o-mini", # Changed from "gpt-4" to "gpt-4o-mini"
|
113 |
+
messages=[
|
114 |
+
{"role": "system", "content": SYSTEM_PROMPT},
|
115 |
+
*[{
|
116 |
+
"role": msg["role"],
|
117 |
+
"content": msg["content"]
|
118 |
+
} for msg in self.conversation_history]
|
119 |
+
],
|
120 |
+
temperature=0.7,
|
121 |
+
max_tokens=2000
|
122 |
+
)
|
123 |
|
124 |
+
return response.choices[0].message.content
|
125 |
|
126 |
+
except Exception as e:
|
127 |
+
logger.warning(f"Attempt {attempt + 1} failed: {str(e)}")
|
128 |
+
if attempt == retries - 1:
|
129 |
+
raise Exception(f"Failed after {retries} attempts: {str(e)}")
|
130 |
+
continue
|
131 |
+
|
132 |
+
def _extract_information(self, text: str) -> List[ExtractedInfo]:
|
133 |
+
"""Extract structured information from text."""
|
134 |
+
try:
|
135 |
+
extraction_prompt = f"""
|
136 |
Analyze the following text and extract relevant information.
|
137 |
Categories to consider: {', '.join(self.extraction_categories)}
|
138 |
|
|
|
155 |
}}
|
156 |
|
157 |
Text to analyze: {text}
|
158 |
+
"""
|
159 |
|
160 |
+
response = self.client.chat.completions.create(
|
161 |
+
model="gpt-4o-mini", # Changed from "gpt-4" to "gpt-4o-mini"
|
162 |
+
messages=[
|
163 |
+
{"role": "system", "content": SYSTEM_PROMPT},
|
164 |
+
{"role": "user", "content": extraction_prompt}
|
165 |
+
],
|
166 |
+
temperature=0.3
|
167 |
+
)
|
168 |
|
169 |
+
# Parse response and create ExtractedInfo objects
|
170 |
+
analysis = json.loads(response.choices[0].message.content)
|
171 |
+
extracted_items = []
|
172 |
|
173 |
+
for item in analysis.get("extracted_items", []):
|
174 |
+
extracted_info = ExtractedInfo(
|
175 |
+
text=item["text"],
|
176 |
+
category=item["category"],
|
177 |
+
confidence=item["confidence"],
|
178 |
+
metadata=item.get("metadata", {})
|
179 |
+
)
|
180 |
+
extracted_items.append(extracted_info)
|
181 |
|
182 |
+
return extracted_items
|
183 |
|
184 |
+
except json.JSONDecodeError as e:
|
185 |
+
logger.error(f"Error parsing extraction response: {str(e)}")
|
186 |
+
return []
|
187 |
+
except Exception as e:
|
188 |
+
logger.error(f"Error during information extraction: {str(e)}")
|
189 |
+
return []
|
190 |
+
|
191 |
+
def _update_completion_status(self) -> None:
|
192 |
+
"""Update completion status based on extracted information."""
|
193 |
+
total_categories = len(self.extraction_categories)
|
194 |
+
covered_categories = len(self.state.categories_covered)
|
195 |
|
196 |
+
# Calculate base completion percentage
|
197 |
+
base_completion = (covered_categories / total_categories) * 100
|
198 |
|
199 |
+
# Adjust based on confidence levels
|
200 |
+
if self.state.extracted_items:
|
201 |
+
avg_confidence = sum(item.confidence for item in self.state.extracted_items) / len(self.state.extracted_items)
|
202 |
+
adjusted_completion = base_completion * avg_confidence
|
203 |
+
else:
|
204 |
+
adjusted_completion = 0.0
|
205 |
|
206 |
+
self.state.completion_percentage = min(adjusted_completion, 100.0)
|
207 |
|
208 |
+
def process_message(self, message: str, api_key: str) -> Dict[str, Any]:
|
209 |
+
"""Process a user message and extract information."""
|
210 |
+
try:
|
211 |
+
# Initialize client if needed
|
212 |
+
if not self.client:
|
213 |
+
self._initialize_client(api_key)
|
214 |
|
215 |
+
# Add user message to history
|
216 |
+
self._add_to_history("user", message)
|
217 |
|
218 |
+
# Get AI response
|
219 |
+
ai_response = self._get_ai_response()
|
220 |
+
self._add_to_history("assistant", ai_response)
|
221 |
|
222 |
+
# Extract information from the entire conversation
|
223 |
+
new_information = self._extract_information(message + "\n" + ai_response)
|
224 |
|
225 |
+
# Update state with new information
|
226 |
+
for info in new_information:
|
227 |
+
self.state.add_extracted_info(info)
|
228 |
|
229 |
+
# Update completion status
|
230 |
+
self._update_completion_status()
|
231 |
|
232 |
+
return {
|
233 |
+
"response": ai_response,
|
234 |
+
"extracted_info": [
|
235 |
+
{
|
236 |
+
"text": info.text,
|
237 |
+
"category": info.category,
|
238 |
+
"confidence": info.confidence
|
239 |
+
} for info in new_information
|
240 |
+
],
|
241 |
+
"completion_status": {
|
242 |
+
"percentage": self.state.completion_percentage,
|
243 |
+
"categories_covered": self.state.categories_covered,
|
244 |
+
"current_focus": self.state.current_focus
|
245 |
+
}
|
246 |
+
}
|
247 |
|
248 |
+
except Exception as e:
|
249 |
+
error_msg = f"Error processing message: {str(e)}"
|
250 |
+
logger.error(error_msg)
|
251 |
+
self.state.last_error = error_msg
|
252 |
+
return {
|
253 |
+
"error": error_msg,
|
254 |
+
"completion_status": {
|
255 |
+
"percentage": self.state.completion_percentage,
|
256 |
+
"categories_covered": self.state.categories_covered,
|
257 |
+
"current_focus": self.state.current_focus
|
258 |
+
}
|
259 |
+
}
|
260 |
+
|
261 |
+
def generate_output(self) -> Dict[str, Any]:
|
262 |
+
"""Generate structured output from all extracted information."""
|
263 |
+
try:
|
264 |
+
# Organize extracted information by category
|
265 |
+
categorized_info = {}
|
266 |
+
for category in self.extraction_categories:
|
267 |
+
category_items = [
|
268 |
+
{
|
269 |
+
"text": item.text,
|
270 |
+
"confidence": item.confidence,
|
271 |
+
"timestamp": item.timestamp.isoformat(),
|
272 |
+
"metadata": item.metadata
|
273 |
+
}
|
274 |
+
for item in self.state.extracted_items
|
275 |
+
if item.category == category
|
276 |
+
]
|
277 |
+
if category_items:
|
278 |
+
categorized_info[category] = category_items
|
279 |
+
|
280 |
+
# Create output structure
|
281 |
+
output = {
|
282 |
+
"extracted_information": categorized_info,
|
283 |
+
"analysis_summary": {
|
284 |
+
"total_items": len(self.state.extracted_items),
|
285 |
+
"categories_covered": self.state.categories_covered,
|
286 |
+
"completion_percentage": self.state.completion_percentage
|
287 |
+
},
|
288 |
+
"metadata": {
|
289 |
+
"generated_at": datetime.now().isoformat(),
|
290 |
+
"conversation_length": len(self.conversation_history),
|
291 |
+
"version": "2.0"
|
292 |
+
}
|
293 |
+
}
|
294 |
+
|
295 |
+
# Save to file
|
296 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
297 |
+
filename = f"extracted_info_{timestamp}.json"
|
298 |
|
299 |
+
with open(filename, 'w', encoding='utf-8') as f:
|
300 |
+
json.dump(output, f, indent=2, ensure_ascii=False)
|
301 |
+
|
302 |
+
return {
|
303 |
+
"filename": filename,
|
304 |
+
"content": output,
|
305 |
+
"status": "success"
|
306 |
+
}
|
307 |
+
|
308 |
+
except Exception as e:
|
309 |
+
error_msg = f"Error generating output: {str(e)}"
|
310 |
+
logger.error(error_msg)
|
311 |
+
return {
|
312 |
+
"error": error_msg,
|
313 |
+
"status": "error"
|
314 |
+
}
|
315 |
+
|
316 |
+
def create_gradio_interface():
|
317 |
+
"""Create the Gradio interface for information extraction."""
|
318 |
+
extractor = InformationExtractor()
|
319 |
|
320 |
+
# Custom CSS for better styling
|
321 |
+
css = """
|
322 |
+
.container { max-width: 900px; margin: auto; }
|
323 |
+
.message { padding: 1rem; margin: 0.5rem 0; border-radius: 0.5rem; }
|
324 |
+
.info-panel { background: #f5f5f5; padding: 1rem; border-radius: 0.5rem; }
|
325 |
+
.status-badge {
|
326 |
+
display: inline-block;
|
327 |
+
padding: 0.25rem 0.5rem;
|
328 |
+
border-radius: 0.25rem;
|
329 |
+
margin: 0.25rem;
|
330 |
+
background: #e0e0e0;
|
331 |
+
}
|
332 |
+
.extraction-highlight {
|
333 |
+
background: #e8f4f8;
|
334 |
+
border-left: 4px solid #4a90e2;
|
335 |
+
padding: 0.5rem;
|
336 |
+
margin: 0.5rem 0;
|
337 |
+
}
|
338 |
+
"""
|
339 |
+
|
340 |
+
with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
|
341 |
+
gr.Markdown("""
|
342 |
# 🔍 Information Extraction Assistant
|
343 |
|
344 |
Have a natural conversation while we extract and organize important information.
|
345 |
The system will automatically identify and categorize relevant details.
|
346 |
+
""")
|
347 |
+
|
348 |
+
with gr.Row():
|
349 |
+
with gr.Column(scale=2):
|
350 |
+
# API Key input
|
351 |
+
api_key = gr.Textbox(
|
352 |
+
label="OpenAI API Key",
|
353 |
+
type="password",
|
354 |
+
placeholder="Enter your OpenAI API key (sk-...)",
|
355 |
+
show_label=True
|
356 |
+
)
|
357 |
+
|
358 |
+
# Chat interface
|
359 |
+
chatbot = gr.Chatbot(
|
360 |
+
value=[],
|
361 |
+
height=400,
|
362 |
+
type="messages",
|
363 |
+
show_label=False
|
364 |
+
)
|
365 |
+
|
366 |
+
# Message input
|
367 |
+
with gr.Row():
|
368 |
+
msg = gr.Textbox(
|
369 |
+
label="Message",
|
370 |
+
placeholder="Type your message here...",
|
371 |
+
scale=4
|
372 |
+
)
|
373 |
+
submit = gr.Button(
|
374 |
+
"Send",
|
375 |
+
variant="primary",
|
376 |
+
scale=1
|
377 |
+
)
|
378 |
+
|
379 |
+
# Action buttons
|
380 |
+
with gr.Row():
|
381 |
+
clear = gr.Button("Clear Chat", scale=1)
|
382 |
+
generate = gr.Button(
|
383 |
+
"Generate Report",
|
384 |
+
variant="secondary",
|
385 |
+
scale=2
|
386 |
+
)
|
387 |
+
|
388 |
+
with gr.Column(scale=1):
|
389 |
+
# Extraction Status Panel
|
390 |
+
with gr.Group(visible=True) as status_panel:
|
391 |
+
gr.Markdown("### Extraction Progress")
|
392 |
|
393 |
+
# Progress indicator
|
394 |
+
progress = gr.Slider(
|
395 |
+
label="Completion",
|
396 |
+
minimum=0,
|
397 |
+
maximum=100,
|
398 |
+
value=0,
|
399 |
+
interactive=False
|
400 |
+
)
|
401 |
|
402 |
+
# Categories covered
|
403 |
+
categories_covered = gr.JSON(
|
404 |
+
label="Categories Covered",
|
405 |
+
value={"categories": []}
|
406 |
+
)
|
407 |
|
408 |
+
# Current focus
|
409 |
+
current_focus = gr.Textbox(
|
410 |
+
label="Current Focus",
|
411 |
+
value="Not started",
|
412 |
+
interactive=False
|
413 |
+
)
|
414 |
+
|
415 |
+
# Extraction Results
|
416 |
+
with gr.Tabs() as result_tabs:
|
417 |
+
with gr.Tab("Extracted Information"):
|
418 |
+
extracted_info = gr.JSON(
|
419 |
+
label="Extracted Details",
|
420 |
+
value={}
|
421 |
+
)
|
422 |
|
423 |
+
with gr.Tab("Download"):
|
424 |
+
file_output = gr.File(
|
425 |
+
label="Download Report"
|
426 |
+
)
|
427 |
|
428 |
+
with gr.Tab("Analysis"):
|
429 |
+
analysis_text = gr.Markdown(
|
430 |
+
"Analysis will appear here after processing."
|
431 |
+
)
|
432 |
+
|
433 |
+
# Helper Functions
|
434 |
+
def format_extraction_summary(extracted_items: List[Dict]) -> str:
|
435 |
+
"""Format extracted information for display."""
|
436 |
+
if not extracted_items:
|
437 |
+
return "No information extracted yet."
|
438 |
|
439 |
+
summary = ["### Recently Extracted Information"]
|
440 |
+
for item in extracted_items:
|
441 |
+
summary.append(
|
442 |
+
f"- **{item['category']}** ({item['confidence']*100:.1f}% confidence)\n"
|
443 |
+
f" {item['text']}"
|
444 |
+
)
|
445 |
+
return "\n".join(summary)
|
446 |
+
|
447 |
+
def update_interface_state(state: Dict[str, Any]) -> tuple:
|
448 |
+
"""Update all interface components based on current state."""
|
449 |
+
return (
|
450 |
+
state['completion_status']['percentage'],
|
451 |
+
{"categories": state['completion_status']['categories_covered']},
|
452 |
+
state['completion_status']['current_focus']
|
453 |
+
)
|
454 |
+
|
455 |
+
# Event Handlers
|
456 |
+
def process_message(message: str, history: list, key: str) -> tuple:
|
457 |
+
"""Handle message processing and update interface."""
|
458 |
+
if not message.strip():
|
459 |
+
return history, 0, {}, "Please enter a message"
|
460 |
|
461 |
+
try:
|
462 |
+
# Process message
|
463 |
+
result = extractor.process_message(message, key)
|
464 |
|
465 |
+
if "error" in result:
|
466 |
+
return (
|
467 |
+
history,
|
468 |
+
0,
|
469 |
+
{"categories": []},
|
470 |
+
f"Error: {result['error']}"
|
471 |
+
)
|
472 |
|
473 |
+
# Update chat history
|
474 |
+
history.append({
|
475 |
+
"role": "user",
|
476 |
+
"content": message
|
477 |
+
})
|
478 |
+
history.append({
|
479 |
+
"role": "assistant",
|
480 |
+
"content": result["response"]
|
481 |
+
})
|
482 |
|
483 |
+
# Update status components
|
484 |
+
progress_value = result["completion_status"]["percentage"]
|
485 |
+
categories = {
|
486 |
+
"categories": result["completion_status"]["categories_covered"]
|
487 |
+
}
|
488 |
+
current_focus = result["completion_status"]["current_focus"] or "Processing..."
|
489 |
|
490 |
+
# Update extraction display
|
491 |
+
if result.get("extracted_info"):
|
492 |
+
analysis_text = format_extraction_summary(result["extracted_info"])
|
493 |
+
else:
|
494 |
+
analysis_text = "No new information extracted."
|
495 |
|
496 |
+
return (
|
497 |
+
history,
|
498 |
+
progress_value,
|
499 |
+
categories,
|
500 |
+
current_focus,
|
501 |
+
analysis_text
|
502 |
+
)
|
503 |
|
504 |
+
except Exception as e:
|
505 |
+
logger.error(f"Error in process_message: {str(e)}")
|
506 |
+
return (
|
507 |
+
history,
|
508 |
+
0,
|
509 |
+
{"categories": []},
|
510 |
+
f"Error: {str(e)}",
|
511 |
+
"An error occurred during processing."
|
512 |
+
)
|
513 |
+
|
514 |
+
def generate_report() -> tuple:
|
515 |
+
"""Generate and return report file."""
|
516 |
+
try:
|
517 |
+
result = extractor.generate_output()
|
518 |
|
519 |
+
if result["status"] == "success":
|
520 |
+
# Update JSON preview
|
521 |
+
content_preview = {
|
522 |
+
"summary": result["content"]["analysis_summary"],
|
523 |
+
"categories": list(result["content"]["extracted_information"].keys()),
|
524 |
+
"total_items": len(result["content"]["extracted_information"])
|
525 |
+
}
|
526 |
|
527 |
+
return (
|
528 |
+
result["filename"],
|
529 |
+
content_preview,
|
530 |
+
"Report generated successfully! 🎉",
|
531 |
+
gr.update(value=format_extraction_summary(
|
532 |
+
[item for items in result["content"]["extracted_information"].values()
|
533 |
+
for item in items]
|
534 |
+
))
|
535 |
+
)
|
536 |
+
else:
|
537 |
+
return (
|
538 |
+
None,
|
539 |
+
{"error": result["error"]},
|
540 |
+
f"Error generating report: {result['error']}",
|
541 |
+
"Failed to generate analysis."
|
542 |
+
)
|
543 |
|
544 |
+
except Exception as e:
|
545 |
+
logger.error(f"Error in generate_report: {str(e)}")
|
546 |
+
return (
|
547 |
+
None,
|
548 |
+
{"error": str(e)},
|
549 |
+
f"Error: {str(e)}",
|
550 |
+
"An error occurred during report generation."
|
551 |
+
)
|
552 |
+
|
553 |
+
def clear_interface() -> tuple:
|
554 |
+
"""Reset all interface components."""
|
555 |
+
# Reset extractor state
|
556 |
+
global extractor
|
557 |
+
extractor = InformationExtractor()
|
558 |
|
559 |
+
return (
|
560 |
+
[], # Clear chat history
|
561 |
+
0.0, # Reset progress
|
562 |
+
{"categories": []}, # Clear categories
|
563 |
+
"Not started", # Reset focus
|
564 |
+
{}, # Clear extracted info
|
565 |
+
None, # Clear file output
|
566 |
+
"Ready to start new extraction.", # Reset analysis
|
567 |
+
gr.update(value="") # Clear message input
|
568 |
+
)
|
569 |
+
|
570 |
+
# Event Bindings
|
571 |
+
msg.submit(
|
572 |
+
process_message,
|
573 |
+
inputs=[msg, chatbot, api_key],
|
574 |
+
outputs=[
|
575 |
+
chatbot,
|
576 |
+
progress,
|
577 |
+
categories_covered,
|
578 |
+
current_focus,
|
579 |
+
analysis_text
|
580 |
+
]
|
581 |
+
).then(
|
582 |
+
lambda: "",
|
583 |
+
None,
|
584 |
+
msg
|
585 |
+
)
|
586 |
+
|
587 |
+
submit.click(
|
588 |
+
process_message,
|
589 |
+
inputs=[msg, chatbot, api_key],
|
590 |
+
outputs=[
|
591 |
+
chatbot,
|
592 |
+
progress,
|
593 |
+
categories_covered,
|
594 |
+
current_focus,
|
595 |
+
analysis_text
|
596 |
+
]
|
597 |
+
).then(
|
598 |
+
lambda: "",
|
599 |
+
None,
|
600 |
+
msg
|
601 |
+
)
|
602 |
+
|
603 |
+
generate.click(
|
604 |
+
generate_report,
|
605 |
+
outputs=[
|
606 |
+
file_output,
|
607 |
+
extracted_info,
|
608 |
+
current_focus,
|
609 |
+
analysis_text
|
610 |
+
]
|
611 |
+
)
|
612 |
+
|
613 |
+
clear.click(
|
614 |
+
clear_interface,
|
615 |
+
outputs=[
|
616 |
+
chatbot,
|
617 |
+
progress,
|
618 |
+
categories_covered,
|
619 |
+
current_focus,
|
620 |
+
extracted_info,
|
621 |
+
file_output,
|
622 |
+
analysis_text,
|
623 |
+
msg
|
624 |
+
]
|
625 |
+
)
|
626 |
+
|
627 |
+
return demo
|
628 |
+
|
629 |
+
if __name__ == "__main__":
|
630 |
+
# Set up logging for the main application
|
631 |
+
logging.basicConfig(
|
632 |
+
level=logging.INFO,
|
633 |
+
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
634 |
+
)
|
635 |
|
636 |
+
try:
|
637 |
+
demo = create_gradio_interface()
|
638 |
+
demo.launch(
|
639 |
+
server_name="0.0.0.0",
|
640 |
+
server_port=7860,
|
641 |
+
share=True,
|
642 |
+
show_api=False
|
643 |
+
)
|
644 |
+
except Exception as e:
|
645 |
+
logger.error(f"Application failed to start: {str(e)}")
|
646 |
+
raise
|