Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,7 +12,7 @@ from datetime import datetime
|
|
| 12 |
# AI Assistant Functions
|
| 13 |
# =============================================================================
|
| 14 |
|
| 15 |
-
def generate_response(prompt: str, temperature: float, max_tokens: int) ->
|
| 16 |
"""Generate AI response with configurable parameters."""
|
| 17 |
time.sleep(1) # Simulate processing time
|
| 18 |
responses = [
|
|
@@ -31,14 +31,19 @@ def generate_response(prompt: str, temperature: float, max_tokens: int) -> str:
|
|
| 31 |
generated_words.append(words[i])
|
| 32 |
time.sleep(0.1) # Simulate streaming effect
|
| 33 |
|
| 34 |
-
|
| 35 |
"How else can I assist you today?",
|
| 36 |
"Is there anything else you'd like to know?",
|
| 37 |
"Feel free to ask more questions!"
|
| 38 |
])
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
def analyze_sentiment(text: str) -> tuple[str,
|
| 41 |
"""Analyze sentiment of the input text."""
|
|
|
|
|
|
|
|
|
|
| 42 |
positive_words = ["good", "great", "excellent", "amazing", "wonderful", "fantastic", "love", "like", "happy", "joy", "success"]
|
| 43 |
negative_words = ["bad", "terrible", "awful", "hate", "dislike", "sad", "angry", "fail", "poor", "worst"]
|
| 44 |
|
|
@@ -50,16 +55,22 @@ def analyze_sentiment(text: str) -> tuple[str, float]:
|
|
| 50 |
score = max(-1, min(1, score)) # Clamp between -1 and 1
|
| 51 |
|
| 52 |
if score > 0.2:
|
| 53 |
-
sentiment = "Positive"
|
| 54 |
elif score < -0.2:
|
| 55 |
-
sentiment = "Negative"
|
| 56 |
else:
|
| 57 |
-
sentiment = "Neutral"
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
-
return
|
| 60 |
|
| 61 |
def translate_text(text: str, target_language: str) -> str:
|
| 62 |
"""Simulate text translation."""
|
|
|
|
|
|
|
|
|
|
| 63 |
translations = {
|
| 64 |
"English": "Hello! How can I help you today?",
|
| 65 |
"Spanish": "¡Hola! ¿Cómo puedo ayudarte hoy?",
|
|
@@ -68,7 +79,29 @@ def translate_text(text: str, target_language: str) -> str:
|
|
| 68 |
"Japanese": "こんにちは!本日どのようにお手伝いできますか?",
|
| 69 |
"Chinese": "你好!今天我能如何帮助你?"
|
| 70 |
}
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
# =============================================================================
|
| 74 |
# Gradio 6 Interface
|
|
@@ -77,7 +110,117 @@ def translate_text(text: str, target_language: str) -> str:
|
|
| 77 |
def create_modern_interface() -> gr.Blocks:
|
| 78 |
"""Create a modern, professional Gradio 6 interface."""
|
| 79 |
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
# Header with branding
|
| 82 |
with gr.Row():
|
| 83 |
with gr.Column(scale=1):
|
|
@@ -108,7 +251,8 @@ def create_modern_interface() -> gr.Blocks:
|
|
| 108 |
user_input = gr.Textbox(
|
| 109 |
placeholder="Type your message here...",
|
| 110 |
label="Your Message",
|
| 111 |
-
elem_classes=["message-input"]
|
|
|
|
| 112 |
)
|
| 113 |
send_btn = gr.Button(
|
| 114 |
"➤ Send",
|
|
@@ -144,7 +288,8 @@ def create_modern_interface() -> gr.Blocks:
|
|
| 144 |
sentiment_btn = gr.Button("📊 Analyze Sentiment")
|
| 145 |
sentiment_output = gr.Textbox(
|
| 146 |
label="Sentiment Analysis",
|
| 147 |
-
interactive=False
|
|
|
|
| 148 |
)
|
| 149 |
with gr.Column(scale=1):
|
| 150 |
translate_btn = gr.Button("🌍 Translate")
|
|
@@ -155,7 +300,8 @@ def create_modern_interface() -> gr.Blocks:
|
|
| 155 |
)
|
| 156 |
translation_output = gr.Textbox(
|
| 157 |
label="Translation",
|
| 158 |
-
interactive=False
|
|
|
|
| 159 |
)
|
| 160 |
|
| 161 |
# File Upload for Document Analysis
|
|
@@ -166,95 +312,73 @@ def create_modern_interface() -> gr.Blocks:
|
|
| 166 |
label="📄 Upload Documents",
|
| 167 |
elem_classes=["file-upload-area"]
|
| 168 |
)
|
|
|
|
|
|
|
| 169 |
analyze_btn = gr.Button(
|
| 170 |
"🔍 Analyze Documents",
|
| 171 |
-
variant="secondary"
|
| 172 |
-
elem_classes=["analyze-button"]
|
| 173 |
)
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
lines=4
|
| 178 |
)
|
| 179 |
-
|
| 180 |
-
# Results Section
|
| 181 |
-
gr.Markdown("---")
|
| 182 |
-
with gr.Row():
|
| 183 |
-
with gr.Column(scale=1):
|
| 184 |
-
# Status Output
|
| 185 |
-
status_output = gr.Markdown(
|
| 186 |
-
label="Status",
|
| 187 |
-
elem_classes=["status-output"]
|
| 188 |
-
)
|
| 189 |
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
label="📥 Download Results",
|
| 193 |
interactive=False,
|
| 194 |
-
|
| 195 |
)
|
| 196 |
|
| 197 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
send_btn.click(
|
| 199 |
fn=generate_response,
|
| 200 |
inputs=[user_input, temperature, max_tokens],
|
| 201 |
outputs=[chatbot, user_input],
|
| 202 |
-
|
| 203 |
-
).then(
|
| 204 |
-
fn=lambda: gr.Textbox(value=""),
|
| 205 |
-
inputs=[],
|
| 206 |
-
outputs=[user_input]
|
| 207 |
)
|
| 208 |
|
| 209 |
user_input.submit(
|
| 210 |
fn=generate_response,
|
| 211 |
inputs=[user_input, temperature, max_tokens],
|
| 212 |
outputs=[chatbot, user_input],
|
| 213 |
-
|
| 214 |
-
).then(
|
| 215 |
-
fn=lambda: gr.Textbox(value=""),
|
| 216 |
-
inputs=[],
|
| 217 |
-
outputs=[user_input]
|
| 218 |
)
|
| 219 |
|
| 220 |
sentiment_btn.click(
|
| 221 |
fn=analyze_sentiment,
|
| 222 |
inputs=[user_input],
|
| 223 |
outputs=[sentiment_output, status_output],
|
| 224 |
-
|
| 225 |
)
|
| 226 |
|
| 227 |
translate_btn.click(
|
| 228 |
fn=translate_text,
|
| 229 |
inputs=[user_input, language_dropdown],
|
| 230 |
outputs=[translation_output],
|
| 231 |
-
|
| 232 |
)
|
| 233 |
|
| 234 |
analyze_btn.click(
|
| 235 |
-
fn=
|
| 236 |
-
f"📊 Analyzed {len(files) if files else 0} documents. "
|
| 237 |
-
"Processing complete! Extracted text, tables, and metadata.",
|
| 238 |
-
f"Analysis completed at {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}"
|
| 239 |
-
),
|
| 240 |
inputs=[file_upload],
|
| 241 |
outputs=[analysis_output, status_output],
|
| 242 |
-
|
| 243 |
)
|
| 244 |
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
return "", None, None, None, None, None
|
| 248 |
-
|
| 249 |
-
gr.ClearButton(
|
| 250 |
-
components=[chatbot, user_input, sentiment_output, translation_output, analysis_output],
|
| 251 |
-
value="🗑️ Clear All",
|
| 252 |
-
variant="stop",
|
| 253 |
-
elem_classes=["clear-button"]
|
| 254 |
-
).click(
|
| 255 |
-
fn=clear_chat,
|
| 256 |
inputs=[],
|
| 257 |
-
outputs=[chatbot, user_input, sentiment_output, translation_output, analysis_output, status_output]
|
|
|
|
| 258 |
)
|
| 259 |
|
| 260 |
return demo
|
|
@@ -269,187 +393,9 @@ if __name__ == "__main__":
|
|
| 269 |
|
| 270 |
# Launch with modern Gradio 6 configuration
|
| 271 |
demo.launch(
|
| 272 |
-
# Modern theme configuration
|
| 273 |
-
theme=gr.themes.Soft(
|
| 274 |
-
primary_hue="blue",
|
| 275 |
-
secondary_hue="indigo",
|
| 276 |
-
neutral_hue="slate",
|
| 277 |
-
font=gr.themes.GoogleFont("Inter"),
|
| 278 |
-
text_size="lg",
|
| 279 |
-
spacing_size="lg",
|
| 280 |
-
radius_size="md"
|
| 281 |
-
).set(
|
| 282 |
-
button_primary_background_fill="*primary_600",
|
| 283 |
-
button_primary_background_fill_hover="*primary_700",
|
| 284 |
-
button_primary_text_color="white",
|
| 285 |
-
block_title_text_weight="600",
|
| 286 |
-
block_label_text_weight="500",
|
| 287 |
-
input_border_color="*neutral_300",
|
| 288 |
-
input_focus_border_color="*primary_500",
|
| 289 |
-
slider_active_color="*primary_500",
|
| 290 |
-
slider_color="*neutral_300"
|
| 291 |
-
),
|
| 292 |
-
|
| 293 |
-
# Custom CSS for enhanced styling
|
| 294 |
-
css="""
|
| 295 |
-
/* Modern Design System */
|
| 296 |
-
.gradio-container {
|
| 297 |
-
max-width: 1200px !important;
|
| 298 |
-
margin: 0 auto !important;
|
| 299 |
-
padding: 2rem !important;
|
| 300 |
-
font-family: 'Inter', sans-serif !important;
|
| 301 |
-
}
|
| 302 |
-
|
| 303 |
-
/* Header Styling */
|
| 304 |
-
.header-text h1 {
|
| 305 |
-
font-size: 2.5rem !important;
|
| 306 |
-
font-weight: 700 !important;
|
| 307 |
-
margin-bottom: 1rem !important;
|
| 308 |
-
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 309 |
-
-webkit-background-clip: text;
|
| 310 |
-
-webkit-text-fill-color: transparent;
|
| 311 |
-
background-clip: text;
|
| 312 |
-
}
|
| 313 |
-
|
| 314 |
-
.header-text p {
|
| 315 |
-
font-size: 1.2rem !important;
|
| 316 |
-
color: #64748b !important;
|
| 317 |
-
line-height: 1.6 !important;
|
| 318 |
-
}
|
| 319 |
-
|
| 320 |
-
/* Chat Container */
|
| 321 |
-
.chat-container {
|
| 322 |
-
border: 1px solid #e2e8f0 !important;
|
| 323 |
-
border-radius: 12px !important;
|
| 324 |
-
background: #ffffff !important;
|
| 325 |
-
box-shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1) !important;
|
| 326 |
-
}
|
| 327 |
-
|
| 328 |
-
/* Message Input */
|
| 329 |
-
.message-input {
|
| 330 |
-
border-radius: 8px !important;
|
| 331 |
-
border: 2px solid #e2e8f0 !important;
|
| 332 |
-
font-size: 1rem !important;
|
| 333 |
-
padding: 0.75rem 1rem !important;
|
| 334 |
-
transition: all 0.3s ease !important;
|
| 335 |
-
}
|
| 336 |
-
|
| 337 |
-
.message-input:focus {
|
| 338 |
-
border-color: #667eea !important;
|
| 339 |
-
box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1) !important;
|
| 340 |
-
}
|
| 341 |
-
|
| 342 |
-
/* Send Button */
|
| 343 |
-
.send-button {
|
| 344 |
-
font-size: 1.1rem !important;
|
| 345 |
-
padding: 0.75rem 2rem !important;
|
| 346 |
-
border-radius: 8px !important;
|
| 347 |
-
font-weight: 600 !important;
|
| 348 |
-
box-shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1) !important;
|
| 349 |
-
transition: all 0.3s ease !important;
|
| 350 |
-
}
|
| 351 |
-
|
| 352 |
-
.send-button:hover {
|
| 353 |
-
transform: translateY(-2px) !important;
|
| 354 |
-
box-shadow: 0 10px 15px -3px rgb(0 0 0 / 0.2) !important;
|
| 355 |
-
}
|
| 356 |
-
|
| 357 |
-
/* Status Output */
|
| 358 |
-
.status-output {
|
| 359 |
-
background: linear-gradient(135deg, #f0f9ff 0%, #e0f2fe 100%) !important;
|
| 360 |
-
border-left: 4px solid #0ea5e9 !important;
|
| 361 |
-
padding: 1rem !important;
|
| 362 |
-
border-radius: 8px !important;
|
| 363 |
-
margin-top: 1rem !important;
|
| 364 |
-
font-weight: 500 !important;
|
| 365 |
-
}
|
| 366 |
-
|
| 367 |
-
/* File Upload Area */
|
| 368 |
-
.file-upload-area {
|
| 369 |
-
border: 2px dashed #cbd5e1 !important;
|
| 370 |
-
border-radius: 12px !important;
|
| 371 |
-
transition: all 0.3s ease !important;
|
| 372 |
-
padding: 1rem !important;
|
| 373 |
-
}
|
| 374 |
-
|
| 375 |
-
.file-upload-area:hover {
|
| 376 |
-
border-color: #667eea !important;
|
| 377 |
-
background: #f8fafc !important;
|
| 378 |
-
}
|
| 379 |
-
|
| 380 |
-
/* Analyze Button */
|
| 381 |
-
.analyze-button {
|
| 382 |
-
margin-top: 1rem !important;
|
| 383 |
-
font-size: 1rem !important;
|
| 384 |
-
padding: 0.5rem 1.5rem !important;
|
| 385 |
-
border-radius: 8px !important;
|
| 386 |
-
font-weight: 500 !important;
|
| 387 |
-
}
|
| 388 |
-
|
| 389 |
-
/* Clear Button */
|
| 390 |
-
.clear-button {
|
| 391 |
-
margin-top: 1rem !important;
|
| 392 |
-
font-size: 1rem !important;
|
| 393 |
-
padding: 0.5rem 1.5rem !important;
|
| 394 |
-
border-radius: 8px !important;
|
| 395 |
-
font-weight: 500 !important;
|
| 396 |
-
}
|
| 397 |
-
|
| 398 |
-
/* Mobile Responsiveness */
|
| 399 |
-
@media (max-width: 768px) {
|
| 400 |
-
.gradio-container {
|
| 401 |
-
padding: 1rem !important;
|
| 402 |
-
}
|
| 403 |
-
|
| 404 |
-
.header-text h1 {
|
| 405 |
-
font-size: 2rem !important;
|
| 406 |
-
}
|
| 407 |
-
|
| 408 |
-
.header-text p {
|
| 409 |
-
font-size: 1rem !important;
|
| 410 |
-
}
|
| 411 |
-
|
| 412 |
-
.send-button {
|
| 413 |
-
width: 100% !important;
|
| 414 |
-
font-size: 1rem !important;
|
| 415 |
-
}
|
| 416 |
-
}
|
| 417 |
-
|
| 418 |
-
/* Dark Mode Support */
|
| 419 |
-
@media (prefers-color-scheme: dark) {
|
| 420 |
-
.chat-container {
|
| 421 |
-
background: #1e293b !important;
|
| 422 |
-
border-color: #334155 !important;
|
| 423 |
-
}
|
| 424 |
-
|
| 425 |
-
.message-input {
|
| 426 |
-
background: #334155 !important;
|
| 427 |
-
border-color: #475569 !important;
|
| 428 |
-
color: #f1f5f9 !important;
|
| 429 |
-
}
|
| 430 |
-
|
| 431 |
-
.message-input:focus {
|
| 432 |
-
border-color: #667eea !important;
|
| 433 |
-
}
|
| 434 |
-
|
| 435 |
-
.status-output {
|
| 436 |
-
background: linear-gradient(135deg, #1e293b 0%, #334155 100%) !important;
|
| 437 |
-
border-left-color: #0ea5e9 !important;
|
| 438 |
-
}
|
| 439 |
-
}
|
| 440 |
-
""",
|
| 441 |
-
|
| 442 |
-
# Footer links with anycoder attribution
|
| 443 |
-
footer_links=[
|
| 444 |
-
{"label": "Built with anycoder", "url": "https://huggingface.co/spaces/akhaliq/anycoder"},
|
| 445 |
-
"gradio",
|
| 446 |
-
"api"
|
| 447 |
-
],
|
| 448 |
-
|
| 449 |
-
# Additional launch parameters
|
| 450 |
server_name="0.0.0.0",
|
| 451 |
server_port=7860,
|
| 452 |
-
share=
|
| 453 |
max_file_size="100mb",
|
| 454 |
show_error=True
|
| 455 |
-
)
|
|
|
|
| 12 |
# AI Assistant Functions
|
| 13 |
# =============================================================================
|
| 14 |
|
| 15 |
+
def generate_response(prompt: str, temperature: float, max_tokens: int) -> tuple:
|
| 16 |
"""Generate AI response with configurable parameters."""
|
| 17 |
time.sleep(1) # Simulate processing time
|
| 18 |
responses = [
|
|
|
|
| 31 |
generated_words.append(words[i])
|
| 32 |
time.sleep(0.1) # Simulate streaming effect
|
| 33 |
|
| 34 |
+
final_response = " ".join(generated_words) + " " + random.choice([
|
| 35 |
"How else can I assist you today?",
|
| 36 |
"Is there anything else you'd like to know?",
|
| 37 |
"Feel free to ask more questions!"
|
| 38 |
])
|
| 39 |
+
|
| 40 |
+
return [[prompt, final_response]], ""
|
| 41 |
|
| 42 |
+
def analyze_sentiment(text: str) -> tuple[str, str]:
|
| 43 |
"""Analyze sentiment of the input text."""
|
| 44 |
+
if not text.strip():
|
| 45 |
+
return "Please enter text to analyze", ""
|
| 46 |
+
|
| 47 |
positive_words = ["good", "great", "excellent", "amazing", "wonderful", "fantastic", "love", "like", "happy", "joy", "success"]
|
| 48 |
negative_words = ["bad", "terrible", "awful", "hate", "dislike", "sad", "angry", "fail", "poor", "worst"]
|
| 49 |
|
|
|
|
| 55 |
score = max(-1, min(1, score)) # Clamp between -1 and 1
|
| 56 |
|
| 57 |
if score > 0.2:
|
| 58 |
+
sentiment = "😊 Positive"
|
| 59 |
elif score < -0.2:
|
| 60 |
+
sentiment = "😟 Negative"
|
| 61 |
else:
|
| 62 |
+
sentiment = "😐 Neutral"
|
| 63 |
+
|
| 64 |
+
result = f"{sentiment}\nScore: {round(score, 2)}"
|
| 65 |
+
status = f"Analysis completed at {datetime.now().strftime('%H:%M:%S')}"
|
| 66 |
|
| 67 |
+
return result, status
|
| 68 |
|
| 69 |
def translate_text(text: str, target_language: str) -> str:
|
| 70 |
"""Simulate text translation."""
|
| 71 |
+
if not text.strip():
|
| 72 |
+
return "Please enter text to translate"
|
| 73 |
+
|
| 74 |
translations = {
|
| 75 |
"English": "Hello! How can I help you today?",
|
| 76 |
"Spanish": "¡Hola! ¿Cómo puedo ayudarte hoy?",
|
|
|
|
| 79 |
"Japanese": "こんにちは!本日どのようにお手伝いできますか?",
|
| 80 |
"Chinese": "你好!今天我能如何帮助你?"
|
| 81 |
}
|
| 82 |
+
|
| 83 |
+
base_translation = translations.get(target_language, f"Translation to {target_language}")
|
| 84 |
+
return f"🌍 Translated to {target_language}:\n\n{base_translation}\n\n(Simulated translation of: '{text[:50]}...')"
|
| 85 |
+
|
| 86 |
+
def analyze_documents(files) -> tuple[str, str]:
|
| 87 |
+
"""Analyze uploaded documents."""
|
| 88 |
+
if not files:
|
| 89 |
+
return "No files uploaded", ""
|
| 90 |
+
|
| 91 |
+
file_count = len(files) if isinstance(files, list) else 1
|
| 92 |
+
result = f"📊 Analyzed {file_count} document{'s' if file_count > 1 else ''}.\n\n"
|
| 93 |
+
result += "✅ Processing complete!\n"
|
| 94 |
+
result += "• Extracted text content\n"
|
| 95 |
+
result += "• Analyzed document structure\n"
|
| 96 |
+
result += "• Generated metadata\n"
|
| 97 |
+
|
| 98 |
+
status = f"📅 Analysis completed at {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}"
|
| 99 |
+
|
| 100 |
+
return result, status
|
| 101 |
+
|
| 102 |
+
def clear_all():
|
| 103 |
+
"""Clear all fields."""
|
| 104 |
+
return None, "", "", "", "", ""
|
| 105 |
|
| 106 |
# =============================================================================
|
| 107 |
# Gradio 6 Interface
|
|
|
|
| 110 |
def create_modern_interface() -> gr.Blocks:
|
| 111 |
"""Create a modern, professional Gradio 6 interface."""
|
| 112 |
|
| 113 |
+
# Define custom theme
|
| 114 |
+
theme = gr.themes.Soft(
|
| 115 |
+
primary_hue="blue",
|
| 116 |
+
secondary_hue="indigo",
|
| 117 |
+
neutral_hue="slate",
|
| 118 |
+
font=gr.themes.GoogleFont("Inter"),
|
| 119 |
+
text_size="lg",
|
| 120 |
+
spacing_size="lg",
|
| 121 |
+
radius_size="md"
|
| 122 |
+
).set(
|
| 123 |
+
button_primary_background_fill="*primary_600",
|
| 124 |
+
button_primary_background_fill_hover="*primary_700",
|
| 125 |
+
button_primary_text_color="white",
|
| 126 |
+
block_title_text_weight="600",
|
| 127 |
+
block_label_text_weight="500",
|
| 128 |
+
input_border_color="*neutral_300",
|
| 129 |
+
input_focus_border_color="*primary_500",
|
| 130 |
+
slider_active_color="*primary_500",
|
| 131 |
+
slider_color="*neutral_300"
|
| 132 |
+
)
|
| 133 |
+
|
| 134 |
+
# Custom CSS
|
| 135 |
+
custom_css = """
|
| 136 |
+
/* Modern Design System */
|
| 137 |
+
.gradio-container {
|
| 138 |
+
max-width: 1200px !important;
|
| 139 |
+
margin: 0 auto !important;
|
| 140 |
+
padding: 2rem !important;
|
| 141 |
+
font-family: 'Inter', sans-serif !important;
|
| 142 |
+
}
|
| 143 |
+
|
| 144 |
+
/* Header Styling */
|
| 145 |
+
.header-text h1 {
|
| 146 |
+
font-size: 2.5rem !important;
|
| 147 |
+
font-weight: 700 !important;
|
| 148 |
+
margin-bottom: 1rem !important;
|
| 149 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 150 |
+
-webkit-background-clip: text;
|
| 151 |
+
-webkit-text-fill-color: transparent;
|
| 152 |
+
background-clip: text;
|
| 153 |
+
}
|
| 154 |
+
|
| 155 |
+
.header-text p {
|
| 156 |
+
font-size: 1.2rem !important;
|
| 157 |
+
color: #64748b !important;
|
| 158 |
+
line-height: 1.6 !important;
|
| 159 |
+
}
|
| 160 |
+
|
| 161 |
+
/* Chat Container */
|
| 162 |
+
.chat-container {
|
| 163 |
+
border: 1px solid #e2e8f0 !important;
|
| 164 |
+
border-radius: 12px !important;
|
| 165 |
+
background: #ffffff !important;
|
| 166 |
+
box-shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1) !important;
|
| 167 |
+
}
|
| 168 |
+
|
| 169 |
+
/* Message Input */
|
| 170 |
+
.message-input textarea {
|
| 171 |
+
border-radius: 8px !important;
|
| 172 |
+
border: 2px solid #e2e8f0 !important;
|
| 173 |
+
font-size: 1rem !important;
|
| 174 |
+
padding: 0.75rem 1rem !important;
|
| 175 |
+
transition: all 0.3s ease !important;
|
| 176 |
+
}
|
| 177 |
+
|
| 178 |
+
.message-input textarea:focus {
|
| 179 |
+
border-color: #667eea !important;
|
| 180 |
+
box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1) !important;
|
| 181 |
+
}
|
| 182 |
+
|
| 183 |
+
/* Buttons */
|
| 184 |
+
.send-button {
|
| 185 |
+
font-size: 1.1rem !important;
|
| 186 |
+
padding: 0.75rem 2rem !important;
|
| 187 |
+
border-radius: 8px !important;
|
| 188 |
+
font-weight: 600 !important;
|
| 189 |
+
box-shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1) !important;
|
| 190 |
+
transition: all 0.3s ease !important;
|
| 191 |
+
}
|
| 192 |
+
|
| 193 |
+
.send-button:hover {
|
| 194 |
+
transform: translateY(-2px) !important;
|
| 195 |
+
box-shadow: 0 10px 15px -3px rgb(0 0 0 / 0.2) !important;
|
| 196 |
+
}
|
| 197 |
+
|
| 198 |
+
/* File Upload Area */
|
| 199 |
+
.file-upload-area {
|
| 200 |
+
border: 2px dashed #cbd5e1 !important;
|
| 201 |
+
border-radius: 12px !important;
|
| 202 |
+
transition: all 0.3s ease !important;
|
| 203 |
+
padding: 1rem !important;
|
| 204 |
+
}
|
| 205 |
+
|
| 206 |
+
.file-upload-area:hover {
|
| 207 |
+
border-color: #667eea !important;
|
| 208 |
+
background: #f8fafc !important;
|
| 209 |
+
}
|
| 210 |
+
|
| 211 |
+
/* Mobile Responsiveness */
|
| 212 |
+
@media (max-width: 768px) {
|
| 213 |
+
.gradio-container {
|
| 214 |
+
padding: 1rem !important;
|
| 215 |
+
}
|
| 216 |
+
|
| 217 |
+
.header-text h1 {
|
| 218 |
+
font-size: 2rem !important;
|
| 219 |
+
}
|
| 220 |
+
}
|
| 221 |
+
"""
|
| 222 |
+
|
| 223 |
+
with gr.Blocks(theme=theme, css=custom_css) as demo:
|
| 224 |
# Header with branding
|
| 225 |
with gr.Row():
|
| 226 |
with gr.Column(scale=1):
|
|
|
|
| 251 |
user_input = gr.Textbox(
|
| 252 |
placeholder="Type your message here...",
|
| 253 |
label="Your Message",
|
| 254 |
+
elem_classes=["message-input"],
|
| 255 |
+
lines=2
|
| 256 |
)
|
| 257 |
send_btn = gr.Button(
|
| 258 |
"➤ Send",
|
|
|
|
| 288 |
sentiment_btn = gr.Button("📊 Analyze Sentiment")
|
| 289 |
sentiment_output = gr.Textbox(
|
| 290 |
label="Sentiment Analysis",
|
| 291 |
+
interactive=False,
|
| 292 |
+
lines=3
|
| 293 |
)
|
| 294 |
with gr.Column(scale=1):
|
| 295 |
translate_btn = gr.Button("🌍 Translate")
|
|
|
|
| 300 |
)
|
| 301 |
translation_output = gr.Textbox(
|
| 302 |
label="Translation",
|
| 303 |
+
interactive=False,
|
| 304 |
+
lines=3
|
| 305 |
)
|
| 306 |
|
| 307 |
# File Upload for Document Analysis
|
|
|
|
| 312 |
label="📄 Upload Documents",
|
| 313 |
elem_classes=["file-upload-area"]
|
| 314 |
)
|
| 315 |
+
|
| 316 |
+
with gr.Row():
|
| 317 |
analyze_btn = gr.Button(
|
| 318 |
"🔍 Analyze Documents",
|
| 319 |
+
variant="secondary"
|
|
|
|
| 320 |
)
|
| 321 |
+
clear_btn = gr.Button(
|
| 322 |
+
"🗑️ Clear All",
|
| 323 |
+
variant="stop"
|
|
|
|
| 324 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 325 |
|
| 326 |
+
analysis_output = gr.Textbox(
|
| 327 |
+
label="Document Analysis",
|
|
|
|
| 328 |
interactive=False,
|
| 329 |
+
lines=6
|
| 330 |
)
|
| 331 |
|
| 332 |
+
# Status Section
|
| 333 |
+
gr.Markdown("---")
|
| 334 |
+
with gr.Row():
|
| 335 |
+
status_output = gr.Textbox(
|
| 336 |
+
label="📊 Status",
|
| 337 |
+
interactive=False,
|
| 338 |
+
lines=2
|
| 339 |
+
)
|
| 340 |
+
|
| 341 |
+
# Event Handlers - CORRIGIDO: Removido api_visibility
|
| 342 |
send_btn.click(
|
| 343 |
fn=generate_response,
|
| 344 |
inputs=[user_input, temperature, max_tokens],
|
| 345 |
outputs=[chatbot, user_input],
|
| 346 |
+
api_name="generate"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 347 |
)
|
| 348 |
|
| 349 |
user_input.submit(
|
| 350 |
fn=generate_response,
|
| 351 |
inputs=[user_input, temperature, max_tokens],
|
| 352 |
outputs=[chatbot, user_input],
|
| 353 |
+
api_name="generate_submit"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 354 |
)
|
| 355 |
|
| 356 |
sentiment_btn.click(
|
| 357 |
fn=analyze_sentiment,
|
| 358 |
inputs=[user_input],
|
| 359 |
outputs=[sentiment_output, status_output],
|
| 360 |
+
api_name="sentiment"
|
| 361 |
)
|
| 362 |
|
| 363 |
translate_btn.click(
|
| 364 |
fn=translate_text,
|
| 365 |
inputs=[user_input, language_dropdown],
|
| 366 |
outputs=[translation_output],
|
| 367 |
+
api_name="translate"
|
| 368 |
)
|
| 369 |
|
| 370 |
analyze_btn.click(
|
| 371 |
+
fn=analyze_documents,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 372 |
inputs=[file_upload],
|
| 373 |
outputs=[analysis_output, status_output],
|
| 374 |
+
api_name="analyze_docs"
|
| 375 |
)
|
| 376 |
|
| 377 |
+
clear_btn.click(
|
| 378 |
+
fn=clear_all,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 379 |
inputs=[],
|
| 380 |
+
outputs=[chatbot, user_input, sentiment_output, translation_output, analysis_output, status_output],
|
| 381 |
+
api_name=False
|
| 382 |
)
|
| 383 |
|
| 384 |
return demo
|
|
|
|
| 393 |
|
| 394 |
# Launch with modern Gradio 6 configuration
|
| 395 |
demo.launch(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 396 |
server_name="0.0.0.0",
|
| 397 |
server_port=7860,
|
| 398 |
+
share=False,
|
| 399 |
max_file_size="100mb",
|
| 400 |
show_error=True
|
| 401 |
+
)
|