Update app.py
Browse files
app.py
CHANGED
|
@@ -2,12 +2,10 @@ import gradio as gr
|
|
| 2 |
import os
|
| 3 |
import json
|
| 4 |
import logging
|
| 5 |
-
from typing import Dict, Any, Optional
|
| 6 |
import tempfile
|
| 7 |
-
from pathlib import Path
|
| 8 |
|
| 9 |
# ══════════════════════════════════════════════════════════════════════════════
|
| 10 |
-
#
|
| 11 |
# ══════════════════════════════════════════════════════════════════════════════
|
| 12 |
|
| 13 |
try:
|
|
@@ -15,596 +13,359 @@ try:
|
|
| 15 |
HF_AVAILABLE = True
|
| 16 |
except ImportError:
|
| 17 |
HF_AVAILABLE = False
|
| 18 |
-
print("⚠️ HuggingFace Hub not available - running in demo mode")
|
| 19 |
|
| 20 |
-
#
|
| 21 |
-
logging.basicConfig(
|
| 22 |
-
format="%(asctime)s | %(levelname)-8s | %(message)s",
|
| 23 |
-
level=logging.INFO,
|
| 24 |
-
datefmt="%Y-%m-%d %H:%M:%S"
|
| 25 |
-
)
|
| 26 |
logger = logging.getLogger(__name__)
|
| 27 |
|
| 28 |
# ══════════════════════════════════════════════════════════════════════════════
|
| 29 |
-
# 🔧
|
| 30 |
# ══════════════════════════════════════════════════════════════════════════════
|
| 31 |
|
| 32 |
-
|
| 33 |
-
"""
|
|
|
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
self.client = None
|
| 38 |
-
|
| 39 |
-
if HF_AVAILABLE and self.api_token:
|
| 40 |
-
try:
|
| 41 |
-
self.client = InferenceClient(api_key=self.api_token)
|
| 42 |
-
logger.info("✅ HuggingFace client initialized successfully")
|
| 43 |
-
except Exception as e:
|
| 44 |
-
logger.error(f"❌ Failed to initialize HF client: {e}")
|
| 45 |
-
self.client = None
|
| 46 |
-
else:
|
| 47 |
-
logger.warning("⚠️ Running in demo mode - no API client available")
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
| 60 |
|
| 61 |
-
#
|
| 62 |
-
|
|
|
|
| 63 |
|
| 64 |
# ══════════════════════════════════════════════════════════════════════════════
|
| 65 |
-
# 🤖
|
| 66 |
# ══════════════════════════════════════════════════════════════════════════════
|
| 67 |
|
| 68 |
-
def
|
| 69 |
-
"""
|
| 70 |
-
if
|
| 71 |
-
return "
|
| 72 |
|
| 73 |
-
if
|
| 74 |
-
return "
|
| 75 |
|
| 76 |
try:
|
| 77 |
# Handle audio input
|
| 78 |
-
if isinstance(
|
| 79 |
-
|
|
|
|
| 80 |
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp_file:
|
| 81 |
import scipy.io.wavfile as wav
|
| 82 |
wav.write(tmp_file.name, sample_rate, audio_array)
|
| 83 |
audio_path = tmp_file.name
|
| 84 |
else:
|
| 85 |
-
audio_path =
|
| 86 |
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
audio_path,
|
| 90 |
model="openai/whisper-large-v3"
|
| 91 |
)
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
return f"🎤 **Transcription:** {text}"
|
| 96 |
-
else:
|
| 97 |
-
return f"❌ **Error:** {result.get('error', 'Unknown error')}"
|
| 98 |
-
|
| 99 |
except Exception as e:
|
| 100 |
-
return f"❌
|
| 101 |
|
| 102 |
-
def
|
| 103 |
-
"""
|
| 104 |
if not messages_json.strip():
|
| 105 |
-
return "
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
try:
|
| 108 |
messages = json.loads(messages_json)
|
| 109 |
-
if not isinstance(messages, list) or not messages:
|
| 110 |
-
return "⚠️ Messages must be a non-empty JSON array."
|
| 111 |
-
|
| 112 |
-
# Validate message structure
|
| 113 |
-
for msg in messages:
|
| 114 |
-
if not isinstance(msg, dict) or "role" not in msg or "content" not in msg:
|
| 115 |
-
return "⚠️ Each message must have 'role' and 'content' fields."
|
| 116 |
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
result = engine.safe_call(
|
| 122 |
-
engine.client.chat.completions.create,
|
| 123 |
-
model="microsoft/DialoGPT-medium",
|
| 124 |
messages=messages,
|
| 125 |
-
max_tokens=150
|
| 126 |
)
|
| 127 |
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
if hasattr(response, "choices") and response.choices:
|
| 131 |
-
reply = response.choices[0].message.content
|
| 132 |
-
return f"🤖 **Assistant:** {reply}"
|
| 133 |
-
else:
|
| 134 |
-
return "🤖 **Assistant:** I'm here to help! How can I assist you today?"
|
| 135 |
-
else:
|
| 136 |
-
return f"❌ **Error:** {result.get('error', 'Unknown error')}"
|
| 137 |
-
|
| 138 |
except json.JSONDecodeError:
|
| 139 |
-
return "
|
| 140 |
except Exception as e:
|
| 141 |
-
return f"❌
|
| 142 |
|
| 143 |
-
def
|
| 144 |
-
"""
|
| 145 |
if not text or "[MASK]" not in text:
|
| 146 |
-
return "
|
| 147 |
-
|
| 148 |
-
if not engine.client:
|
| 149 |
-
# Demo response based on common patterns
|
| 150 |
-
demo_predictions = []
|
| 151 |
-
if "programming language" in text.lower():
|
| 152 |
-
demo_predictions = ["Python", "JavaScript", "Java", "C++", "Go"]
|
| 153 |
-
elif "capital" in text.lower():
|
| 154 |
-
demo_predictions = ["Paris", "London", "Tokyo", "Berlin", "Madrid"]
|
| 155 |
-
else:
|
| 156 |
-
demo_predictions = ["amazing", "powerful", "useful", "innovative", "advanced"]
|
| 157 |
-
|
| 158 |
-
formatted_results = []
|
| 159 |
-
for i, pred in enumerate(demo_predictions, 1):
|
| 160 |
-
formatted_results.append(f"{i}. **{pred}** (demo prediction)")
|
| 161 |
-
|
| 162 |
-
return "🎭 **Demo Predictions:**\n" + "\n".join(formatted_results) + "\n\n📝 *Configure HF_API_TOKEN for real predictions.*"
|
| 163 |
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
text,
|
| 167 |
-
model="google-bert/bert-base-uncased"
|
| 168 |
-
)
|
| 169 |
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
token = pred.get("token_str", "").strip()
|
| 176 |
score = pred.get("score", 0)
|
| 177 |
-
|
| 178 |
-
return "🎭 **
|
| 179 |
else:
|
| 180 |
-
return f"🎭 **Result:** {
|
| 181 |
-
|
| 182 |
-
|
|
|
|
| 183 |
|
| 184 |
-
def
|
| 185 |
-
"""
|
| 186 |
if not question or not context:
|
| 187 |
-
return "
|
| 188 |
-
|
| 189 |
-
if not engine.client:
|
| 190 |
-
# Simple demo logic
|
| 191 |
-
if "benefit" in question.lower() and "cloud" in context.lower():
|
| 192 |
-
return "💡 **Demo Answer:** Scalability and cost-effectiveness\n📊 **Demo Confidence:** 0.95\n\n📝 *Configure HF_API_TOKEN for real Q&A.*"
|
| 193 |
-
else:
|
| 194 |
-
return f"💡 **Demo Answer:** Based on the context, the answer relates to your question about '{question[:50]}...'\n📝 *Configure HF_API_TOKEN for real Q&A.*"
|
| 195 |
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
question=question.strip(),
|
| 199 |
-
context=context.strip(),
|
| 200 |
-
model="deepset/roberta-base-squad2"
|
| 201 |
-
)
|
| 202 |
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
answer =
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
|
| 211 |
-
def
|
| 212 |
-
"""
|
| 213 |
if not text or len(text.split()) < 10:
|
| 214 |
-
return "
|
| 215 |
-
|
| 216 |
-
if not engine.client:
|
| 217 |
-
# Simple demo summarization
|
| 218 |
-
sentences = text.split('. ')
|
| 219 |
-
if len(sentences) > 2:
|
| 220 |
-
summary = sentences[0] + ". " + sentences[1] + "."
|
| 221 |
-
else:
|
| 222 |
-
summary = text[:100] + "..."
|
| 223 |
-
return f"📝 **Demo Summary:** {summary}\n\n📝 *Configure HF_API_TOKEN for real summarization.*"
|
| 224 |
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
text.strip(),
|
| 228 |
-
model="facebook/bart-large-cnn",
|
| 229 |
-
max_length=130,
|
| 230 |
-
min_length=30
|
| 231 |
-
)
|
| 232 |
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 237 |
else:
|
| 238 |
-
summary = str(
|
|
|
|
| 239 |
return f"📝 **Summary:** {summary}"
|
| 240 |
-
|
| 241 |
-
|
|
|
|
| 242 |
|
| 243 |
-
def
|
| 244 |
-
"""
|
| 245 |
if not prompt.strip():
|
| 246 |
-
return "
|
| 247 |
|
| 248 |
-
if
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
for key, value in demo_continuations.items():
|
| 258 |
-
if key.lower() in prompt.lower():
|
| 259 |
-
continuation = value
|
| 260 |
-
break
|
| 261 |
|
| 262 |
-
|
| 263 |
-
continuation = "filled with exciting possibilities and innovations that will benefit humanity."
|
| 264 |
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
result = engine.safe_call(
|
| 268 |
-
engine.client.text_generation,
|
| 269 |
-
prompt.strip(),
|
| 270 |
-
model="gpt2",
|
| 271 |
-
max_new_tokens=100,
|
| 272 |
-
temperature=0.7
|
| 273 |
-
)
|
| 274 |
-
|
| 275 |
-
if result.get("success"):
|
| 276 |
-
generated = result["data"]
|
| 277 |
-
if isinstance(generated, list) and generated:
|
| 278 |
-
text = generated[0].get("generated_text", prompt)
|
| 279 |
-
else:
|
| 280 |
-
text = str(generated)
|
| 281 |
-
return f"✍️ **Generated Text:** {text}"
|
| 282 |
-
else:
|
| 283 |
-
return f"❌ **Error:** {result.get('error', 'Unknown error')}"
|
| 284 |
|
| 285 |
-
def
|
| 286 |
-
"""
|
| 287 |
if image_path is None:
|
| 288 |
-
return "
|
| 289 |
-
|
| 290 |
-
if not engine.client:
|
| 291 |
-
return "🖼️ **Demo Classification:**\n1. **Object** (85%)\n2. **Scene** (12%)\n3. **Other** (3%)\n\n📝 *Configure HF_API_TOKEN for real image classification.*"
|
| 292 |
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
image_path,
|
| 296 |
-
model="google/vit-base-patch16-224"
|
| 297 |
-
)
|
| 298 |
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 304 |
label = pred.get("label", "Unknown")
|
| 305 |
score = pred.get("score", 0)
|
| 306 |
-
|
| 307 |
-
return "🖼️ **
|
| 308 |
else:
|
| 309 |
-
return f"🖼️ **Result:** {
|
| 310 |
-
|
| 311 |
-
|
|
|
|
| 312 |
|
| 313 |
-
def
|
| 314 |
-
"""
|
| 315 |
if not text.strip():
|
| 316 |
-
return "
|
| 317 |
-
|
| 318 |
-
if not engine.client:
|
| 319 |
-
import hashlib
|
| 320 |
-
# Generate a pseudo-embedding for demo
|
| 321 |
-
text_hash = hashlib.md5(text.encode()).hexdigest()
|
| 322 |
-
demo_values = [round(float(int(text_hash[i:i+2], 16)) / 255, 4) for i in range(0, min(len(text_hash), 10), 2)]
|
| 323 |
-
return f"🧮 **Demo Feature Vector:** Dimension: 384\n**Sample values:** {demo_values}...\n\n📝 *Configure HF_API_TOKEN for real embeddings.*"
|
| 324 |
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
text.strip(),
|
| 328 |
-
model="sentence-transformers/all-MiniLM-L6-v2"
|
| 329 |
-
)
|
| 330 |
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 337 |
else:
|
| 338 |
-
return f"🧮 **
|
| 339 |
-
|
| 340 |
-
|
|
|
|
| 341 |
|
| 342 |
# ══════════════════════════════════════════════════════════════════════════════
|
| 343 |
-
# 🎨 GRADIO INTERFACE
|
| 344 |
# ══════════════════════════════════════════════════════════════════════════════
|
| 345 |
|
| 346 |
-
|
| 347 |
-
"""Create the main Gradio interface optimized for HuggingFace Spaces."""
|
| 348 |
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
.
|
| 357 |
-
|
| 358 |
-
}
|
| 359 |
-
"""
|
| 360 |
-
) as demo:
|
| 361 |
-
|
| 362 |
gr.Markdown("""
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
""")
|
|
|
|
|
|
|
| 368 |
|
| 369 |
-
#
|
| 370 |
-
|
| 371 |
-
gr.
|
| 372 |
-
|
| 373 |
-
|
|
|
|
|
|
|
| 374 |
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
sources=["upload", "microphone"],
|
| 383 |
-
type="numpy",
|
| 384 |
-
label="📁 Upload or 🎙️ Record Audio"
|
| 385 |
-
)
|
| 386 |
-
asr_button = gr.Button("🔄 Process Audio", variant="primary")
|
| 387 |
-
|
| 388 |
-
with gr.Column(scale=1):
|
| 389 |
-
asr_output = gr.Textbox(
|
| 390 |
-
label="📝 Transcription Result",
|
| 391 |
-
lines=4,
|
| 392 |
-
placeholder="Audio transcription will appear here..."
|
| 393 |
-
)
|
| 394 |
-
|
| 395 |
-
asr_button.click(
|
| 396 |
-
fn=process_speech_recognition,
|
| 397 |
-
inputs=[asr_input],
|
| 398 |
-
outputs=[asr_output]
|
| 399 |
-
)
|
| 400 |
-
|
| 401 |
-
# 💬 Chat Tab
|
| 402 |
-
with gr.TabItem("💬 Chat"):
|
| 403 |
-
with gr.Row():
|
| 404 |
-
with gr.Column(scale=1):
|
| 405 |
-
chat_input = gr.Textbox(
|
| 406 |
-
label="💭 Messages (JSON Format)",
|
| 407 |
-
lines=5,
|
| 408 |
-
placeholder='[{"role":"user","content":"Hello, how are you?"}]',
|
| 409 |
-
value='[{"role":"user","content":"Hello! Tell me about artificial intelligence."}]'
|
| 410 |
-
)
|
| 411 |
-
chat_button = gr.Button("💬 Send Message", variant="primary")
|
| 412 |
-
|
| 413 |
-
gr.Markdown("""
|
| 414 |
-
**📝 Format Examples:**
|
| 415 |
-
- `[{"role":"user","content":"Your message here"}]`
|
| 416 |
-
- `[{"role":"system","content":"You are helpful"},{"role":"user","content":"Hi"}]`
|
| 417 |
-
""")
|
| 418 |
-
|
| 419 |
-
with gr.Column(scale=1):
|
| 420 |
-
chat_output = gr.Textbox(
|
| 421 |
-
label="🤖 AI Response",
|
| 422 |
-
lines=6,
|
| 423 |
-
placeholder="AI response will appear here..."
|
| 424 |
-
)
|
| 425 |
-
|
| 426 |
-
chat_button.click(
|
| 427 |
-
fn=process_chat,
|
| 428 |
-
inputs=[chat_input],
|
| 429 |
-
outputs=[chat_output]
|
| 430 |
-
)
|
| 431 |
-
|
| 432 |
-
# 🎭 Fill Mask Tab
|
| 433 |
-
with gr.TabItem("🎭 Fill Mask"):
|
| 434 |
-
with gr.Row():
|
| 435 |
-
with gr.Column(scale=1):
|
| 436 |
-
mask_input = gr.Textbox(
|
| 437 |
-
label="🎯 Text with [MASK] Token",
|
| 438 |
-
lines=3,
|
| 439 |
-
placeholder="The capital of France is [MASK].",
|
| 440 |
-
value="The most popular programming language is [MASK]."
|
| 441 |
-
)
|
| 442 |
-
mask_button = gr.Button("🔍 Predict Mask", variant="primary")
|
| 443 |
-
|
| 444 |
-
with gr.Column(scale=1):
|
| 445 |
-
mask_output = gr.Textbox(
|
| 446 |
-
label="🎭 Predictions",
|
| 447 |
-
lines=6,
|
| 448 |
-
placeholder="Mask predictions will appear here..."
|
| 449 |
-
)
|
| 450 |
-
|
| 451 |
-
mask_button.click(
|
| 452 |
-
fn=process_fill_mask,
|
| 453 |
-
inputs=[mask_input],
|
| 454 |
-
outputs=[mask_output]
|
| 455 |
-
)
|
| 456 |
-
|
| 457 |
-
# ❓ Question Answering Tab
|
| 458 |
-
with gr.TabItem("❓ Q&A"):
|
| 459 |
-
with gr.Row():
|
| 460 |
-
with gr.Column(scale=1):
|
| 461 |
-
qa_question = gr.Textbox(
|
| 462 |
-
label="❓ Question",
|
| 463 |
-
placeholder="What is machine learning?",
|
| 464 |
-
value="What is the main benefit of cloud computing?"
|
| 465 |
-
)
|
| 466 |
-
qa_context = gr.Textbox(
|
| 467 |
-
label="📚 Context",
|
| 468 |
-
lines=5,
|
| 469 |
-
placeholder="Provide context for the question...",
|
| 470 |
-
value="Cloud computing is a technology that allows users to access computing resources like servers, storage, and applications over the internet. It offers scalability, cost-effectiveness, and flexibility for businesses and individuals."
|
| 471 |
-
)
|
| 472 |
-
qa_button = gr.Button("🔍 Find Answer", variant="primary")
|
| 473 |
-
|
| 474 |
-
with gr.Column(scale=1):
|
| 475 |
-
qa_output = gr.Textbox(
|
| 476 |
-
label="💡 Answer",
|
| 477 |
-
lines=6,
|
| 478 |
-
placeholder="Answer will appear here..."
|
| 479 |
-
)
|
| 480 |
-
|
| 481 |
-
qa_button.click(
|
| 482 |
-
fn=process_question_answering,
|
| 483 |
-
inputs=[qa_question, qa_context],
|
| 484 |
-
outputs=[qa_output]
|
| 485 |
)
|
| 486 |
-
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
|
| 495 |
-
|
| 496 |
-
)
|
| 497 |
-
sum_button = gr.Button("📝 Summarize", variant="primary")
|
| 498 |
-
|
| 499 |
-
with gr.Column(scale=1):
|
| 500 |
-
sum_output = gr.Textbox(
|
| 501 |
-
label="✨ Summary",
|
| 502 |
-
lines=6,
|
| 503 |
-
placeholder="Summary will appear here..."
|
| 504 |
-
)
|
| 505 |
-
|
| 506 |
-
sum_button.click(
|
| 507 |
-
fn=process_summarization,
|
| 508 |
-
inputs=[sum_input],
|
| 509 |
-
outputs=[sum_output]
|
| 510 |
)
|
| 511 |
-
|
| 512 |
-
|
| 513 |
-
|
| 514 |
-
|
| 515 |
-
|
| 516 |
-
|
| 517 |
-
|
| 518 |
-
|
| 519 |
-
|
| 520 |
-
|
| 521 |
-
|
| 522 |
-
|
| 523 |
-
|
| 524 |
-
with gr.Column(scale=1):
|
| 525 |
-
gen_output = gr.Textbox(
|
| 526 |
-
label="🎨 Generated Content",
|
| 527 |
-
lines=6,
|
| 528 |
-
placeholder="Generated text will appear here..."
|
| 529 |
-
)
|
| 530 |
-
|
| 531 |
-
gen_button.click(
|
| 532 |
-
fn=process_text_generation,
|
| 533 |
-
inputs=[gen_input],
|
| 534 |
-
outputs=[gen_output]
|
| 535 |
)
|
| 536 |
-
|
| 537 |
-
|
| 538 |
-
|
| 539 |
-
|
| 540 |
-
|
| 541 |
-
|
| 542 |
-
|
| 543 |
-
|
| 544 |
-
|
| 545 |
-
|
| 546 |
-
|
| 547 |
-
with gr.Column(scale=1):
|
| 548 |
-
img_output = gr.Textbox(
|
| 549 |
-
label="🏷️ Classification Results",
|
| 550 |
-
lines=6,
|
| 551 |
-
placeholder="Image classification results will appear here..."
|
| 552 |
-
)
|
| 553 |
-
|
| 554 |
-
img_button.click(
|
| 555 |
-
fn=process_image_classification,
|
| 556 |
-
inputs=[img_input],
|
| 557 |
-
outputs=[img_output]
|
| 558 |
)
|
| 559 |
-
|
| 560 |
-
|
| 561 |
-
|
| 562 |
-
|
| 563 |
-
|
| 564 |
-
|
| 565 |
-
|
| 566 |
-
|
| 567 |
-
|
| 568 |
-
|
| 569 |
-
)
|
| 570 |
-
fe_button = gr.Button("🧮 Extract Features", variant="primary")
|
| 571 |
-
|
| 572 |
-
with gr.Column(scale=1):
|
| 573 |
-
fe_output = gr.Textbox(
|
| 574 |
-
label="🔢 Feature Vector",
|
| 575 |
-
lines=6,
|
| 576 |
-
placeholder="Feature vectors will appear here..."
|
| 577 |
-
)
|
| 578 |
-
|
| 579 |
-
fe_button.click(
|
| 580 |
-
fn=process_feature_extraction,
|
| 581 |
-
inputs=[fe_input],
|
| 582 |
-
outputs=[fe_output]
|
| 583 |
)
|
|
|
|
|
|
|
|
|
|
| 584 |
|
| 585 |
-
#
|
| 586 |
-
gr.
|
| 587 |
-
|
| 588 |
-
|
|
|
|
|
|
|
|
|
|
| 589 |
|
| 590 |
-
|
| 591 |
-
""
|
| 592 |
-
|
| 593 |
-
|
| 594 |
-
|
| 595 |
-
|
| 596 |
-
|
| 597 |
-
|
|
|
|
|
|
|
| 598 |
|
| 599 |
if __name__ == "__main__":
|
| 600 |
-
|
| 601 |
-
|
| 602 |
-
app = create_interface()
|
| 603 |
-
app.launch(
|
| 604 |
server_name="0.0.0.0",
|
| 605 |
server_port=7860,
|
| 606 |
-
share=False
|
| 607 |
-
|
| 608 |
-
inbrowser=True
|
| 609 |
-
)
|
| 610 |
-
|
|
|
|
| 2 |
import os
|
| 3 |
import json
|
| 4 |
import logging
|
|
|
|
| 5 |
import tempfile
|
|
|
|
| 6 |
|
| 7 |
# ══════════════════════════════════════════════════════════════════════════════
|
| 8 |
+
# 🎓 ACADEMIC RESEARCH AI DEMO - SIMPLE & CLEAN
|
| 9 |
# ══════════════════════════════════════════════════════════════════════════════
|
| 10 |
|
| 11 |
try:
|
|
|
|
| 13 |
HF_AVAILABLE = True
|
| 14 |
except ImportError:
|
| 15 |
HF_AVAILABLE = False
|
|
|
|
| 16 |
|
| 17 |
+
# Simple logging
|
| 18 |
+
logging.basicConfig(level=logging.INFO)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
logger = logging.getLogger(__name__)
|
| 20 |
|
| 21 |
# ══════════════════════════════════════════════════════════════════════════════
|
| 22 |
+
# 🔧 SIMPLE CLIENT SETUP (Following Official Docs)
|
| 23 |
# ══════════════════════════════════════════════════════════════════════════════
|
| 24 |
|
| 25 |
+
def get_client():
|
| 26 |
+
"""Get HuggingFace Inference Client following official documentation."""
|
| 27 |
+
api_token = os.getenv("HF_API_TOKEN")
|
| 28 |
|
| 29 |
+
if not HF_AVAILABLE:
|
| 30 |
+
return None, "HuggingFace Hub not installed"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
if not api_token:
|
| 33 |
+
return None, "HF_API_TOKEN not set"
|
| 34 |
+
|
| 35 |
+
try:
|
| 36 |
+
# Following official docs - using provider parameter
|
| 37 |
+
client = InferenceClient(
|
| 38 |
+
provider="hf-inference",
|
| 39 |
+
api_key=api_token,
|
| 40 |
+
)
|
| 41 |
+
return client, "Connected"
|
| 42 |
+
except Exception as e:
|
| 43 |
+
return None, f"Connection failed: {e}"
|
| 44 |
|
| 45 |
+
# Initialize client
|
| 46 |
+
CLIENT, STATUS = get_client()
|
| 47 |
+
logger.info(f"Client status: {STATUS}")
|
| 48 |
|
| 49 |
# ══════════════════════════════════════════════════════════════════════════════
|
| 50 |
+
# 🤖 SIMPLE TASK FUNCTIONS (No Async)
|
| 51 |
# ══════════════════════════════════════════════════════════════════════════════
|
| 52 |
|
| 53 |
+
def speech_recognition(audio_file):
|
| 54 |
+
"""Speech Recognition - Following official docs exactly."""
|
| 55 |
+
if audio_file is None:
|
| 56 |
+
return "❌ Please upload an audio file"
|
| 57 |
|
| 58 |
+
if CLIENT is None:
|
| 59 |
+
return f"❌ Client not available: {STATUS}"
|
| 60 |
|
| 61 |
try:
|
| 62 |
# Handle audio input
|
| 63 |
+
if isinstance(audio_file, tuple):
|
| 64 |
+
# Convert numpy array to file if needed
|
| 65 |
+
sample_rate, audio_array = audio_file
|
| 66 |
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp_file:
|
| 67 |
import scipy.io.wavfile as wav
|
| 68 |
wav.write(tmp_file.name, sample_rate, audio_array)
|
| 69 |
audio_path = tmp_file.name
|
| 70 |
else:
|
| 71 |
+
audio_path = audio_file
|
| 72 |
|
| 73 |
+
# Official API call from docs
|
| 74 |
+
output = CLIENT.automatic_speech_recognition(
|
| 75 |
+
audio_path,
|
| 76 |
model="openai/whisper-large-v3"
|
| 77 |
)
|
| 78 |
|
| 79 |
+
return f"🎤 **Transcription:** {output.get('text', str(output))}"
|
| 80 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
except Exception as e:
|
| 82 |
+
return f"❌ Error: {str(e)}"
|
| 83 |
|
| 84 |
+
def chat_completion(messages_json):
|
| 85 |
+
"""Chat Completion - Following official docs exactly."""
|
| 86 |
if not messages_json.strip():
|
| 87 |
+
return "❌ Please enter valid JSON messages"
|
| 88 |
+
|
| 89 |
+
if CLIENT is None:
|
| 90 |
+
return f"❌ Client not available: {STATUS}"
|
| 91 |
|
| 92 |
try:
|
| 93 |
messages = json.loads(messages_json)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
+
# Official API call from docs
|
| 96 |
+
completion = CLIENT.chat.completions.create(
|
| 97 |
+
model="meta-llama/Llama-3.2-3B-Instruct",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
messages=messages,
|
|
|
|
| 99 |
)
|
| 100 |
|
| 101 |
+
return f"🤖 **Response:** {completion.choices[0].message.content}"
|
| 102 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
except json.JSONDecodeError:
|
| 104 |
+
return "❌ Invalid JSON format"
|
| 105 |
except Exception as e:
|
| 106 |
+
return f"❌ Error: {str(e)}"
|
| 107 |
|
| 108 |
+
def fill_mask(text):
|
| 109 |
+
"""Fill Mask - Following official docs exactly."""
|
| 110 |
if not text or "[MASK]" not in text:
|
| 111 |
+
return "❌ Text must contain [MASK] token"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
+
if CLIENT is None:
|
| 114 |
+
return f"❌ Client not available: {STATUS}"
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
+
try:
|
| 117 |
+
# Official API call from docs
|
| 118 |
+
result = CLIENT.fill_mask(
|
| 119 |
+
text,
|
| 120 |
+
model="google-bert/bert-base-uncased",
|
| 121 |
+
)
|
| 122 |
+
|
| 123 |
+
if isinstance(result, list):
|
| 124 |
+
predictions = []
|
| 125 |
+
for i, pred in enumerate(result[:5], 1):
|
| 126 |
token = pred.get("token_str", "").strip()
|
| 127 |
score = pred.get("score", 0)
|
| 128 |
+
predictions.append(f"{i}. **{token}** ({score:.3f})")
|
| 129 |
+
return "🎭 **Predictions:**\n" + "\n".join(predictions)
|
| 130 |
else:
|
| 131 |
+
return f"🎭 **Result:** {result}"
|
| 132 |
+
|
| 133 |
+
except Exception as e:
|
| 134 |
+
return f"❌ Error: {str(e)}"
|
| 135 |
|
| 136 |
+
def question_answering(question, context):
|
| 137 |
+
"""Question Answering - Following official docs exactly."""
|
| 138 |
if not question or not context:
|
| 139 |
+
return "❌ Both question and context required"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
|
| 141 |
+
if CLIENT is None:
|
| 142 |
+
return f"❌ Client not available: {STATUS}"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
|
| 144 |
+
try:
|
| 145 |
+
# Official API call from docs
|
| 146 |
+
answer = CLIENT.question_answering(
|
| 147 |
+
question=question,
|
| 148 |
+
context=context,
|
| 149 |
+
model="deepset/roberta-base-squad2",
|
| 150 |
+
)
|
| 151 |
+
|
| 152 |
+
return f"💡 **Answer:** {answer.get('answer', str(answer))}\n📊 **Score:** {answer.get('score', 'N/A')}"
|
| 153 |
+
|
| 154 |
+
except Exception as e:
|
| 155 |
+
return f"❌ Error: {str(e)}"
|
| 156 |
|
| 157 |
+
def summarization(text):
|
| 158 |
+
"""Summarization - Following official docs exactly."""
|
| 159 |
if not text or len(text.split()) < 10:
|
| 160 |
+
return "❌ Please provide at least 10 words"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
|
| 162 |
+
if CLIENT is None:
|
| 163 |
+
return f"❌ Client not available: {STATUS}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
|
| 165 |
+
try:
|
| 166 |
+
# Official API call from docs
|
| 167 |
+
result = CLIENT.summarization(
|
| 168 |
+
text,
|
| 169 |
+
model="facebook/bart-large-cnn",
|
| 170 |
+
)
|
| 171 |
+
|
| 172 |
+
if isinstance(result, list) and result:
|
| 173 |
+
summary = result[0].get("summary_text", str(result))
|
| 174 |
else:
|
| 175 |
+
summary = str(result)
|
| 176 |
+
|
| 177 |
return f"📝 **Summary:** {summary}"
|
| 178 |
+
|
| 179 |
+
except Exception as e:
|
| 180 |
+
return f"❌ Error: {str(e)}"
|
| 181 |
|
| 182 |
+
def text_generation(prompt):
|
| 183 |
+
"""Text Generation - Following official docs exactly."""
|
| 184 |
if not prompt.strip():
|
| 185 |
+
return "❌ Prompt cannot be empty"
|
| 186 |
|
| 187 |
+
if CLIENT is None:
|
| 188 |
+
return f"❌ Client not available: {STATUS}"
|
| 189 |
+
|
| 190 |
+
try:
|
| 191 |
+
# Official API call from docs
|
| 192 |
+
completion = CLIENT.chat.completions.create(
|
| 193 |
+
model="meta-llama/Llama-3.2-3B-Instruct",
|
| 194 |
+
messages=[{"role": "user", "content": prompt}],
|
| 195 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
|
| 197 |
+
return f"✍️ **Generated:** {completion.choices[0].message.content}"
|
|
|
|
| 198 |
|
| 199 |
+
except Exception as e:
|
| 200 |
+
return f"❌ Error: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
|
| 202 |
+
def image_classification(image_path):
|
| 203 |
+
"""Image Classification - Following official docs exactly."""
|
| 204 |
if image_path is None:
|
| 205 |
+
return "❌ Please upload an image"
|
|
|
|
|
|
|
|
|
|
| 206 |
|
| 207 |
+
if CLIENT is None:
|
| 208 |
+
return f"❌ Client not available: {STATUS}"
|
|
|
|
|
|
|
|
|
|
| 209 |
|
| 210 |
+
try:
|
| 211 |
+
# Official API call from docs
|
| 212 |
+
output = CLIENT.image_classification(
|
| 213 |
+
image_path,
|
| 214 |
+
model="google/vit-base-patch16-224"
|
| 215 |
+
)
|
| 216 |
+
|
| 217 |
+
if isinstance(output, list):
|
| 218 |
+
results = []
|
| 219 |
+
for i, pred in enumerate(output[:5], 1):
|
| 220 |
label = pred.get("label", "Unknown")
|
| 221 |
score = pred.get("score", 0)
|
| 222 |
+
results.append(f"{i}. **{label}** ({score:.1%})")
|
| 223 |
+
return "🖼️ **Classifications:**\n" + "\n".join(results)
|
| 224 |
else:
|
| 225 |
+
return f"🖼️ **Result:** {output}"
|
| 226 |
+
|
| 227 |
+
except Exception as e:
|
| 228 |
+
return f"❌ Error: {str(e)}"
|
| 229 |
|
| 230 |
+
def feature_extraction(text):
|
| 231 |
+
"""Feature Extraction - Following official docs exactly."""
|
| 232 |
if not text.strip():
|
| 233 |
+
return "❌ Text cannot be empty"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 234 |
|
| 235 |
+
if CLIENT is None:
|
| 236 |
+
return f"❌ Client not available: {STATUS}"
|
|
|
|
|
|
|
|
|
|
| 237 |
|
| 238 |
+
try:
|
| 239 |
+
# Official API call from docs
|
| 240 |
+
result = CLIENT.feature_extraction(
|
| 241 |
+
text,
|
| 242 |
+
model="intfloat/multilingual-e5-large-instruct",
|
| 243 |
+
)
|
| 244 |
+
|
| 245 |
+
if isinstance(result, list) and result:
|
| 246 |
+
dim = len(result[0]) if result[0] else 0
|
| 247 |
+
sample = result[0][:5] if dim >= 5 else result[0]
|
| 248 |
+
return f"🧮 **Embeddings:** Dimension: {dim}\n**Sample:** {sample}..."
|
| 249 |
else:
|
| 250 |
+
return f"🧮 **Result:** {str(result)[:200]}..."
|
| 251 |
+
|
| 252 |
+
except Exception as e:
|
| 253 |
+
return f"❌ Error: {str(e)}"
|
| 254 |
|
| 255 |
# ══════════════════════════════════════════════════════════════════════════════
|
| 256 |
+
# 🎨 SIMPLE GRADIO INTERFACE
|
| 257 |
# ══════════════════════════════════════════════════════════════════════════════
|
| 258 |
|
| 259 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="AI Research Demo") as demo:
|
|
|
|
| 260 |
|
| 261 |
+
gr.Markdown("""
|
| 262 |
+
# 🎓 AI Research & Academic Demo
|
| 263 |
+
### Simple HuggingFace Inference API Implementation
|
| 264 |
+
""")
|
| 265 |
+
|
| 266 |
+
# Status display
|
| 267 |
+
if CLIENT:
|
| 268 |
+
gr.Markdown("✅ **Status:** Connected to HuggingFace Inference API")
|
| 269 |
+
else:
|
| 270 |
+
gr.Markdown(f"❌ **Status:** {STATUS}")
|
|
|
|
|
|
|
|
|
|
| 271 |
gr.Markdown("""
|
| 272 |
+
**Setup Instructions:**
|
| 273 |
+
1. Get your API key from: https://huggingface.co/settings/tokens
|
| 274 |
+
2. Set environment variable: `HF_API_TOKEN=your_token_here`
|
| 275 |
+
3. Restart the application
|
| 276 |
""")
|
| 277 |
+
|
| 278 |
+
with gr.Tabs():
|
| 279 |
|
| 280 |
+
# Speech Recognition
|
| 281 |
+
with gr.TabItem("🎤 Speech Recognition"):
|
| 282 |
+
with gr.Row():
|
| 283 |
+
asr_input = gr.Audio(sources=["upload", "microphone"], type="numpy")
|
| 284 |
+
asr_output = gr.Textbox(label="Transcription", lines=3)
|
| 285 |
+
asr_btn = gr.Button("Transcribe")
|
| 286 |
+
asr_btn.click(speech_recognition, asr_input, asr_output)
|
| 287 |
|
| 288 |
+
# Chat
|
| 289 |
+
with gr.TabItem("💬 Chat"):
|
| 290 |
+
with gr.Row():
|
| 291 |
+
chat_input = gr.Textbox(
|
| 292 |
+
label="Messages (JSON)",
|
| 293 |
+
lines=3,
|
| 294 |
+
value='[{"role":"user","content":"What is machine learning?"}]'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 295 |
)
|
| 296 |
+
chat_output = gr.Textbox(label="Response", lines=5)
|
| 297 |
+
chat_btn = gr.Button("Send")
|
| 298 |
+
chat_btn.click(chat_completion, chat_input, chat_output)
|
| 299 |
+
|
| 300 |
+
# Fill Mask
|
| 301 |
+
with gr.TabItem("🎭 Fill Mask"):
|
| 302 |
+
with gr.Row():
|
| 303 |
+
mask_input = gr.Textbox(
|
| 304 |
+
label="Text with [MASK]",
|
| 305 |
+
value="The capital of France is [MASK]."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 306 |
)
|
| 307 |
+
mask_output = gr.Textbox(label="Predictions", lines=5)
|
| 308 |
+
mask_btn = gr.Button("Predict")
|
| 309 |
+
mask_btn.click(fill_mask, mask_input, mask_output)
|
| 310 |
+
|
| 311 |
+
# Q&A
|
| 312 |
+
with gr.TabItem("❓ Q&A"):
|
| 313 |
+
with gr.Column():
|
| 314 |
+
qa_question = gr.Textbox(label="Question", value="What is AI?")
|
| 315 |
+
qa_context = gr.Textbox(
|
| 316 |
+
label="Context",
|
| 317 |
+
lines=3,
|
| 318 |
+
value="Artificial Intelligence (AI) is the simulation of human intelligence in machines."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 319 |
)
|
| 320 |
+
qa_output = gr.Textbox(label="Answer", lines=3)
|
| 321 |
+
qa_btn = gr.Button("Answer")
|
| 322 |
+
qa_btn.click(question_answering, [qa_question, qa_context], qa_output)
|
| 323 |
+
|
| 324 |
+
# Summarization
|
| 325 |
+
with gr.TabItem("📝 Summarization"):
|
| 326 |
+
with gr.Row():
|
| 327 |
+
sum_input = gr.Textbox(
|
| 328 |
+
label="Text to Summarize",
|
| 329 |
+
lines=5,
|
| 330 |
+
value="Machine learning is a method of data analysis that automates analytical model building. It is a branch of artificial intelligence based on the idea that systems can learn from data, identify patterns and make decisions with minimal human intervention."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 331 |
)
|
| 332 |
+
sum_output = gr.Textbox(label="Summary", lines=3)
|
| 333 |
+
sum_btn = gr.Button("Summarize")
|
| 334 |
+
sum_btn.click(summarization, sum_input, sum_output)
|
| 335 |
+
|
| 336 |
+
# Text Generation
|
| 337 |
+
with gr.TabItem("✍️ Text Generation"):
|
| 338 |
+
with gr.Row():
|
| 339 |
+
gen_input = gr.Textbox(
|
| 340 |
+
label="Prompt",
|
| 341 |
+
value="The future of AI research will be"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 342 |
)
|
| 343 |
+
gen_output = gr.Textbox(label="Generated Text", lines=5)
|
| 344 |
+
gen_btn = gr.Button("Generate")
|
| 345 |
+
gen_btn.click(text_generation, gen_input, gen_output)
|
| 346 |
|
| 347 |
+
# Image Classification
|
| 348 |
+
with gr.TabItem("🖼️ Image Classification"):
|
| 349 |
+
with gr.Row():
|
| 350 |
+
img_input = gr.Image(type="filepath")
|
| 351 |
+
img_output = gr.Textbox(label="Classification", lines=5)
|
| 352 |
+
img_btn = gr.Button("Classify")
|
| 353 |
+
img_btn.click(image_classification, img_input, img_output)
|
| 354 |
|
| 355 |
+
# Feature Extraction
|
| 356 |
+
with gr.TabItem("🧮 Feature Extraction"):
|
| 357 |
+
with gr.Row():
|
| 358 |
+
fe_input = gr.Textbox(
|
| 359 |
+
label="Text",
|
| 360 |
+
value="This is a sample text for feature extraction."
|
| 361 |
+
)
|
| 362 |
+
fe_output = gr.Textbox(label="Features", lines=5)
|
| 363 |
+
fe_btn = gr.Button("Extract")
|
| 364 |
+
fe_btn.click(feature_extraction, fe_input, fe_output)
|
| 365 |
|
| 366 |
if __name__ == "__main__":
|
| 367 |
+
demo.launch(
|
|
|
|
|
|
|
|
|
|
| 368 |
server_name="0.0.0.0",
|
| 369 |
server_port=7860,
|
| 370 |
+
share=False
|
| 371 |
+
)
|
|
|
|
|
|
|
|
|