Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,14 @@ import gradio as gr
|
|
| 2 |
import os
|
| 3 |
import json
|
| 4 |
import logging
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 7 |
# ๐ MINIMAL AI RESEARCH DEMO - GRADIO 5.0.1 COMPATIBLE
|
|
@@ -25,26 +33,36 @@ def get_client():
|
|
| 25 |
"""Get HuggingFace client - exactly like your working example."""
|
| 26 |
api_token = os.getenv("HF_API_TOKEN")
|
| 27 |
|
| 28 |
-
if not HF_AVAILABLE
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
return None
|
| 30 |
|
| 31 |
try:
|
|
|
|
| 32 |
client = InferenceClient(
|
| 33 |
provider="hf-inference",
|
| 34 |
api_key=api_token,
|
| 35 |
)
|
|
|
|
| 36 |
# Test with exact same call as your working example
|
|
|
|
| 37 |
test_result = client.fill_mask("The capital of France is [MASK].", model="google-bert/bert-base-uncased")
|
| 38 |
-
logger.info(f"โ
Client test successful: {type(test_result)}")
|
|
|
|
| 39 |
return client
|
|
|
|
| 40 |
except Exception as e:
|
| 41 |
-
logger.error(f"โ Client failed: {e}")
|
| 42 |
return None
|
| 43 |
|
| 44 |
CLIENT = get_client()
|
| 45 |
|
| 46 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 47 |
-
# ๐ค
|
| 48 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 49 |
|
| 50 |
def run_chat(message):
|
|
@@ -52,26 +70,51 @@ def run_chat(message):
|
|
| 52 |
if not CLIENT:
|
| 53 |
return "โ Client not available"
|
| 54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
try:
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
completion = CLIENT.chat.completions.create(
|
| 58 |
model="Qwen/Qwen2.5-72B-Instruct",
|
| 59 |
messages=messages,
|
| 60 |
)
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
except Exception as e:
|
| 63 |
-
|
|
|
|
| 64 |
|
| 65 |
def run_fill_mask(text):
|
| 66 |
"""Fill mask - using your exact working approach."""
|
| 67 |
if not CLIENT:
|
| 68 |
return "โ Client not available"
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
try:
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
if isinstance(result, list):
|
| 76 |
output = "๐ญ **Predictions:**\n"
|
| 77 |
for i, pred in enumerate(result[:5], 1):
|
|
@@ -80,52 +123,361 @@ def run_fill_mask(text):
|
|
| 80 |
output += f"{i}. **{token}** ({score:.3f})\n"
|
| 81 |
return output
|
| 82 |
return str(result)
|
|
|
|
| 83 |
except Exception as e:
|
| 84 |
-
|
|
|
|
| 85 |
|
| 86 |
def run_question_answering(question, context):
|
| 87 |
-
"""Q&A function."""
|
| 88 |
-
if not CLIENT
|
| 89 |
-
return "โ Client not available
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
answer = CLIENT.question_answering(
|
| 93 |
-
question=
|
| 94 |
-
context=
|
| 95 |
model="deepset/roberta-base-squad2",
|
| 96 |
)
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
except Exception as e:
|
| 99 |
-
|
|
|
|
| 100 |
|
| 101 |
def run_summarization(text):
|
| 102 |
-
"""Summarization function."""
|
| 103 |
-
if not CLIENT
|
| 104 |
-
return "โ Client not available
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
try:
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
if isinstance(result, list) and result:
|
| 109 |
-
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
except Exception as e:
|
|
|
|
| 112 |
return f"โ Error: {str(e)}"
|
| 113 |
|
| 114 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 115 |
-
# ๐จ
|
| 116 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 117 |
|
| 118 |
-
# Create interface with
|
| 119 |
-
with gr.Blocks(title="AI Research
|
| 120 |
|
| 121 |
-
gr.Markdown("#
|
| 122 |
|
| 123 |
if CLIENT:
|
| 124 |
gr.Markdown("โ
**Status:** Connected and tested successfully")
|
| 125 |
else:
|
| 126 |
gr.Markdown("โ **Status:** Set HF_API_TOKEN environment variable")
|
| 127 |
|
| 128 |
-
#
|
|
|
|
|
|
|
|
|
|
| 129 |
gr.Markdown("## ๐ฌ Chat with AI")
|
| 130 |
chat_input = gr.Textbox(label="Your Message", placeholder="Ask anything...")
|
| 131 |
chat_output = gr.Textbox(label="AI Response", lines=5)
|
|
@@ -134,8 +486,11 @@ with gr.Blocks(title="AI Research Demo") as demo:
|
|
| 134 |
|
| 135 |
gr.Markdown("---")
|
| 136 |
|
| 137 |
-
#
|
| 138 |
-
|
|
|
|
|
|
|
|
|
|
| 139 |
mask_input = gr.Textbox(
|
| 140 |
label="Text with [MASK]",
|
| 141 |
value="The capital of France is [MASK].",
|
|
@@ -147,7 +502,6 @@ with gr.Blocks(title="AI Research Demo") as demo:
|
|
| 147 |
|
| 148 |
gr.Markdown("---")
|
| 149 |
|
| 150 |
-
# Q&A
|
| 151 |
gr.Markdown("## โ Question Answering")
|
| 152 |
qa_question = gr.Textbox(label="Question", value="What is AI?")
|
| 153 |
qa_context = gr.Textbox(
|
|
@@ -161,7 +515,10 @@ with gr.Blocks(title="AI Research Demo") as demo:
|
|
| 161 |
|
| 162 |
gr.Markdown("---")
|
| 163 |
|
| 164 |
-
#
|
|
|
|
|
|
|
|
|
|
| 165 |
gr.Markdown("## ๐ Text Summarization")
|
| 166 |
sum_input = gr.Textbox(
|
| 167 |
label="Text to Summarize",
|
|
@@ -173,11 +530,178 @@ with gr.Blocks(title="AI Research Demo") as demo:
|
|
| 173 |
sum_btn.click(run_summarization, inputs=sum_input, outputs=sum_output)
|
| 174 |
|
| 175 |
gr.Markdown("---")
|
| 176 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
|
| 178 |
if __name__ == "__main__":
|
| 179 |
demo.launch(
|
| 180 |
server_name="0.0.0.0",
|
| 181 |
server_port=7860,
|
| 182 |
share=False
|
| 183 |
-
)
|
|
|
|
|
|
| 2 |
import os
|
| 3 |
import json
|
| 4 |
import logging
|
| 5 |
+
import tempfile
|
| 6 |
+
|
| 7 |
+
# Optional imports for audio processing
|
| 8 |
+
try:
|
| 9 |
+
import scipy.io.wavfile
|
| 10 |
+
SCIPY_AVAILABLE = True
|
| 11 |
+
except ImportError:
|
| 12 |
+
SCIPY_AVAILABLE = False
|
| 13 |
|
| 14 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 15 |
# ๐ MINIMAL AI RESEARCH DEMO - GRADIO 5.0.1 COMPATIBLE
|
|
|
|
| 33 |
"""Get HuggingFace client - exactly like your working example."""
|
| 34 |
api_token = os.getenv("HF_API_TOKEN")
|
| 35 |
|
| 36 |
+
if not HF_AVAILABLE:
|
| 37 |
+
logger.error("โ HuggingFace Hub not available")
|
| 38 |
+
return None
|
| 39 |
+
|
| 40 |
+
if not api_token:
|
| 41 |
+
logger.error("โ HF_API_TOKEN not set")
|
| 42 |
return None
|
| 43 |
|
| 44 |
try:
|
| 45 |
+
logger.info("๐ Initializing HuggingFace client...")
|
| 46 |
client = InferenceClient(
|
| 47 |
provider="hf-inference",
|
| 48 |
api_key=api_token,
|
| 49 |
)
|
| 50 |
+
|
| 51 |
# Test with exact same call as your working example
|
| 52 |
+
logger.info("๐งช Testing client with fill_mask...")
|
| 53 |
test_result = client.fill_mask("The capital of France is [MASK].", model="google-bert/bert-base-uncased")
|
| 54 |
+
logger.info(f"โ
Client test successful: {type(test_result)}, got {len(test_result) if isinstance(test_result, list) else 'non-list'} results")
|
| 55 |
+
|
| 56 |
return client
|
| 57 |
+
|
| 58 |
except Exception as e:
|
| 59 |
+
logger.error(f"โ Client initialization failed: {e}")
|
| 60 |
return None
|
| 61 |
|
| 62 |
CLIENT = get_client()
|
| 63 |
|
| 64 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 65 |
+
# ๐ค COMPREHENSIVE AI FUNCTIONS - ALL HUGGINGFACE CAPABILITIES
|
| 66 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 67 |
|
| 68 |
def run_chat(message):
|
|
|
|
| 70 |
if not CLIENT:
|
| 71 |
return "โ Client not available"
|
| 72 |
|
| 73 |
+
# Handle None/empty message from Gradio
|
| 74 |
+
if not message or str(message).strip() == "":
|
| 75 |
+
return "โ Please enter a message"
|
| 76 |
+
|
| 77 |
try:
|
| 78 |
+
clean_message = str(message).strip()
|
| 79 |
+
messages = [{"role": "user", "content": clean_message}]
|
| 80 |
+
|
| 81 |
+
logger.info(f"Chat call: message='{clean_message}'")
|
| 82 |
+
|
| 83 |
completion = CLIENT.chat.completions.create(
|
| 84 |
model="Qwen/Qwen2.5-72B-Instruct",
|
| 85 |
messages=messages,
|
| 86 |
)
|
| 87 |
+
|
| 88 |
+
result = completion.choices[0].message.content
|
| 89 |
+
logger.info(f"Chat result: {type(result)}")
|
| 90 |
+
|
| 91 |
+
return result
|
| 92 |
+
|
| 93 |
except Exception as e:
|
| 94 |
+
logger.error(f"Chat error: {e}")
|
| 95 |
+
return f"โ Error: {str(e)}\n\n**Debug:** Message: '{message}'"
|
| 96 |
|
| 97 |
def run_fill_mask(text):
|
| 98 |
"""Fill mask - using your exact working approach."""
|
| 99 |
if not CLIENT:
|
| 100 |
return "โ Client not available"
|
| 101 |
|
| 102 |
+
# Handle None/empty text from Gradio
|
| 103 |
+
if not text or str(text).strip() == "":
|
| 104 |
+
return "โ Please enter text with [MASK]"
|
| 105 |
+
|
| 106 |
+
clean_text = str(text).strip()
|
| 107 |
+
|
| 108 |
+
if "[MASK]" not in clean_text:
|
| 109 |
+
return "โ Text must contain [MASK] token"
|
| 110 |
|
| 111 |
try:
|
| 112 |
+
logger.info(f"Fill mask call: text='{clean_text}'")
|
| 113 |
+
|
| 114 |
+
result = CLIENT.fill_mask(clean_text, model="google-bert/bert-base-uncased")
|
| 115 |
+
|
| 116 |
+
logger.info(f"Fill mask result: {type(result)}")
|
| 117 |
+
|
| 118 |
if isinstance(result, list):
|
| 119 |
output = "๐ญ **Predictions:**\n"
|
| 120 |
for i, pred in enumerate(result[:5], 1):
|
|
|
|
| 123 |
output += f"{i}. **{token}** ({score:.3f})\n"
|
| 124 |
return output
|
| 125 |
return str(result)
|
| 126 |
+
|
| 127 |
except Exception as e:
|
| 128 |
+
logger.error(f"Fill mask error: {e}")
|
| 129 |
+
return f"โ Error: {str(e)}\n\n**Debug:** Text: '{text}'"
|
| 130 |
|
| 131 |
def run_question_answering(question, context):
|
| 132 |
+
"""Q&A function - Fixed parameter handling."""
|
| 133 |
+
if not CLIENT:
|
| 134 |
+
return "โ Client not available"
|
| 135 |
+
|
| 136 |
+
# Handle None/empty values from Gradio
|
| 137 |
+
if not question or not context or question.strip() == "" or context.strip() == "":
|
| 138 |
+
return "โ Please provide both question and context"
|
| 139 |
|
| 140 |
try:
|
| 141 |
+
# Ensure we have clean string inputs
|
| 142 |
+
clean_question = str(question).strip()
|
| 143 |
+
clean_context = str(context).strip()
|
| 144 |
+
|
| 145 |
+
logger.info(f"Q&A call: question='{clean_question}', context='{clean_context[:50]}...'")
|
| 146 |
+
|
| 147 |
answer = CLIENT.question_answering(
|
| 148 |
+
question=clean_question,
|
| 149 |
+
context=clean_context,
|
| 150 |
model="deepset/roberta-base-squad2",
|
| 151 |
)
|
| 152 |
+
|
| 153 |
+
logger.info(f"Q&A result: {type(answer)}, {answer}")
|
| 154 |
+
|
| 155 |
+
if isinstance(answer, dict):
|
| 156 |
+
return f"๐ก **Answer:** {answer.get('answer', 'No answer found')}\n๐ **Score:** {answer.get('score', 'N/A')}"
|
| 157 |
+
else:
|
| 158 |
+
return f"๐ก **Answer:** {str(answer)}"
|
| 159 |
+
|
| 160 |
except Exception as e:
|
| 161 |
+
logger.error(f"Q&A error: {e}")
|
| 162 |
+
return f"โ Error: {str(e)}\n\n**Debug:** Question: '{question}', Context: '{context[:50] if context else 'None'}...'"
|
| 163 |
|
| 164 |
def run_summarization(text):
|
| 165 |
+
"""Summarization function - Fixed parameter handling."""
|
| 166 |
+
if not CLIENT:
|
| 167 |
+
return "โ Client not available"
|
| 168 |
+
|
| 169 |
+
# Handle None/empty text from Gradio
|
| 170 |
+
if not text or str(text).strip() == "":
|
| 171 |
+
return "โ Please enter text to summarize"
|
| 172 |
+
|
| 173 |
+
clean_text = str(text).strip()
|
| 174 |
+
|
| 175 |
+
if len(clean_text.split()) < 10:
|
| 176 |
+
return "โ Text must be at least 10 words long"
|
| 177 |
|
| 178 |
try:
|
| 179 |
+
logger.info(f"Summarization call: text length={len(clean_text)}")
|
| 180 |
+
|
| 181 |
+
result = CLIENT.summarization(clean_text, model="facebook/bart-large-cnn")
|
| 182 |
+
|
| 183 |
+
logger.info(f"Summarization result: {type(result)}")
|
| 184 |
+
|
| 185 |
if isinstance(result, list) and result:
|
| 186 |
+
summary = result[0].get('summary_text', str(result[0]))
|
| 187 |
+
return f"๐ **Summary:** {summary}"
|
| 188 |
+
elif isinstance(result, dict):
|
| 189 |
+
summary = result.get('summary_text', str(result))
|
| 190 |
+
return f"๐ **Summary:** {summary}"
|
| 191 |
+
else:
|
| 192 |
+
return f"๐ **Summary:** {str(result)}"
|
| 193 |
+
|
| 194 |
+
except Exception as e:
|
| 195 |
+
logger.error(f"Summarization error: {e}")
|
| 196 |
+
return f"โ Error: {str(e)}\n\n**Debug:** Text length: {len(text) if text else 0}"
|
| 197 |
+
|
| 198 |
+
def run_text_classification(text):
|
| 199 |
+
"""Text Classification function."""
|
| 200 |
+
if not CLIENT:
|
| 201 |
+
return "โ Client not available"
|
| 202 |
+
|
| 203 |
+
if not text or str(text).strip() == "":
|
| 204 |
+
return "โ Please enter text to classify"
|
| 205 |
+
|
| 206 |
+
clean_text = str(text).strip()
|
| 207 |
+
|
| 208 |
+
try:
|
| 209 |
+
logger.info(f"Text classification call: text='{clean_text[:50]}...'")
|
| 210 |
+
|
| 211 |
+
result = CLIENT.text_classification(clean_text, model="cardiffnlp/twitter-roberta-base-sentiment-latest")
|
| 212 |
+
|
| 213 |
+
logger.info(f"Text classification result: {type(result)}")
|
| 214 |
+
|
| 215 |
+
if isinstance(result, list):
|
| 216 |
+
output = "๐ท๏ธ **Classifications:**\n"
|
| 217 |
+
for i, pred in enumerate(result[:5], 1):
|
| 218 |
+
label = pred.get("label", "Unknown")
|
| 219 |
+
score = pred.get("score", 0)
|
| 220 |
+
output += f"{i}. **{label}** ({score:.3f})\n"
|
| 221 |
+
return output
|
| 222 |
+
return str(result)
|
| 223 |
+
|
| 224 |
+
except Exception as e:
|
| 225 |
+
logger.error(f"Text classification error: {e}")
|
| 226 |
+
return f"โ Error: {str(e)}"
|
| 227 |
+
|
| 228 |
+
def run_token_classification(text):
|
| 229 |
+
"""Token Classification (NER) function."""
|
| 230 |
+
if not CLIENT:
|
| 231 |
+
return "โ Client not available"
|
| 232 |
+
|
| 233 |
+
if not text or str(text).strip() == "":
|
| 234 |
+
return "โ Please enter text for entity recognition"
|
| 235 |
+
|
| 236 |
+
clean_text = str(text).strip()
|
| 237 |
+
|
| 238 |
+
try:
|
| 239 |
+
logger.info(f"Token classification call: text='{clean_text}'")
|
| 240 |
+
|
| 241 |
+
result = CLIENT.token_classification(clean_text, model="dslim/bert-base-NER")
|
| 242 |
+
|
| 243 |
+
logger.info(f"Token classification result: {type(result)}")
|
| 244 |
+
|
| 245 |
+
if isinstance(result, list):
|
| 246 |
+
output = "๐ท๏ธ **Named Entities:**\n"
|
| 247 |
+
for i, entity in enumerate(result[:10], 1):
|
| 248 |
+
word = entity.get("word", "Unknown")
|
| 249 |
+
label = entity.get("entity", "Unknown")
|
| 250 |
+
score = entity.get("score", 0)
|
| 251 |
+
output += f"{i}. **{word}** โ {label} ({score:.3f})\n"
|
| 252 |
+
return output
|
| 253 |
+
return str(result)
|
| 254 |
+
|
| 255 |
+
except Exception as e:
|
| 256 |
+
logger.error(f"Token classification error: {e}")
|
| 257 |
+
return f"โ Error: {str(e)}"
|
| 258 |
+
|
| 259 |
+
def run_translation(text):
|
| 260 |
+
"""Translation function."""
|
| 261 |
+
if not CLIENT:
|
| 262 |
+
return "โ Client not available"
|
| 263 |
+
|
| 264 |
+
if not text or str(text).strip() == "":
|
| 265 |
+
return "โ Please enter text to translate"
|
| 266 |
+
|
| 267 |
+
clean_text = str(text).strip()
|
| 268 |
+
|
| 269 |
+
try:
|
| 270 |
+
logger.info(f"Translation call: text='{clean_text[:50]}...'")
|
| 271 |
+
|
| 272 |
+
result = CLIENT.translation(clean_text, model="Helsinki-NLP/opus-mt-en-fr")
|
| 273 |
+
|
| 274 |
+
logger.info(f"Translation result: {type(result)}")
|
| 275 |
+
|
| 276 |
+
if isinstance(result, list) and result:
|
| 277 |
+
translation = result[0].get('translation_text', str(result[0]))
|
| 278 |
+
return f"๐ **Translation (ENโFR):** {translation}"
|
| 279 |
+
elif isinstance(result, dict):
|
| 280 |
+
translation = result.get('translation_text', str(result))
|
| 281 |
+
return f"๐ **Translation (ENโFR):** {translation}"
|
| 282 |
+
else:
|
| 283 |
+
return f"๐ **Translation:** {str(result)}"
|
| 284 |
+
|
| 285 |
+
except Exception as e:
|
| 286 |
+
logger.error(f"Translation error: {e}")
|
| 287 |
+
return f"โ Error: {str(e)}"
|
| 288 |
+
|
| 289 |
+
def run_feature_extraction(text):
|
| 290 |
+
"""Feature Extraction function."""
|
| 291 |
+
if not CLIENT:
|
| 292 |
+
return "โ Client not available"
|
| 293 |
+
|
| 294 |
+
if not text or str(text).strip() == "":
|
| 295 |
+
return "โ Please enter text for feature extraction"
|
| 296 |
+
|
| 297 |
+
clean_text = str(text).strip()
|
| 298 |
+
|
| 299 |
+
try:
|
| 300 |
+
logger.info(f"Feature extraction call: text='{clean_text[:50]}...'")
|
| 301 |
+
|
| 302 |
+
result = CLIENT.feature_extraction(clean_text, model="intfloat/multilingual-e5-large-instruct")
|
| 303 |
+
|
| 304 |
+
logger.info(f"Feature extraction result: {type(result)}")
|
| 305 |
+
|
| 306 |
+
if isinstance(result, list) and result:
|
| 307 |
+
dim = len(result[0]) if result[0] else 0
|
| 308 |
+
sample = result[0][:5] if dim >= 5 else result[0]
|
| 309 |
+
return f"๐งฎ **Feature Vector:** Dimension: {dim}\n**Sample values:** {sample}..."
|
| 310 |
+
else:
|
| 311 |
+
return f"๐งฎ **Features:** {str(result)[:200]}..."
|
| 312 |
+
|
| 313 |
+
except Exception as e:
|
| 314 |
+
logger.error(f"Feature extraction error: {e}")
|
| 315 |
+
return f"โ Error: {str(e)}"
|
| 316 |
+
|
| 317 |
+
def run_zero_shot_classification(text, labels):
|
| 318 |
+
"""Zero Shot Classification function."""
|
| 319 |
+
if not CLIENT:
|
| 320 |
+
return "โ Client not available"
|
| 321 |
+
|
| 322 |
+
if not text or str(text).strip() == "":
|
| 323 |
+
return "โ Please enter text to classify"
|
| 324 |
+
|
| 325 |
+
if not labels or str(labels).strip() == "":
|
| 326 |
+
return "โ Please enter candidate labels (comma-separated)"
|
| 327 |
+
|
| 328 |
+
clean_text = str(text).strip()
|
| 329 |
+
clean_labels = [label.strip() for label in str(labels).split(",") if label.strip()]
|
| 330 |
+
|
| 331 |
+
if not clean_labels:
|
| 332 |
+
return "โ Please provide valid labels separated by commas"
|
| 333 |
+
|
| 334 |
+
try:
|
| 335 |
+
logger.info(f"Zero shot classification call: text='{clean_text[:50]}...', labels={clean_labels}")
|
| 336 |
+
|
| 337 |
+
# Use text classification with BART MNLI model for zero-shot
|
| 338 |
+
result = CLIENT.text_classification(
|
| 339 |
+
clean_text,
|
| 340 |
+
model="facebook/bart-large-mnli",
|
| 341 |
+
# Note: Some models support candidate_labels parameter
|
| 342 |
+
)
|
| 343 |
+
|
| 344 |
+
logger.info(f"Zero shot classification result: {type(result)}")
|
| 345 |
+
|
| 346 |
+
if isinstance(result, list):
|
| 347 |
+
output = "๐ฏ **Zero-Shot Classification:**\n"
|
| 348 |
+
for i, pred in enumerate(result[:5], 1):
|
| 349 |
+
label = pred.get("label", "Unknown")
|
| 350 |
+
score = pred.get("score", 0)
|
| 351 |
+
output += f"{i}. **{label}** ({score:.3f})\n"
|
| 352 |
+
return output
|
| 353 |
+
elif isinstance(result, dict):
|
| 354 |
+
labels_result = result.get('labels', [])
|
| 355 |
+
scores = result.get('scores', [])
|
| 356 |
+
output = "๐ฏ **Zero-Shot Classification:**\n"
|
| 357 |
+
for i, (label, score) in enumerate(zip(labels_result[:5], scores[:5]), 1):
|
| 358 |
+
output += f"{i}. **{label}** ({score:.3f})\n"
|
| 359 |
+
return output
|
| 360 |
+
else:
|
| 361 |
+
return f"๐ฏ **Result:** {str(result)}"
|
| 362 |
+
|
| 363 |
+
except Exception as e:
|
| 364 |
+
logger.error(f"Zero shot classification error: {e}")
|
| 365 |
+
# Fallback to regular text classification
|
| 366 |
+
try:
|
| 367 |
+
result = CLIENT.text_classification(clean_text, model="cardiffnlp/twitter-roberta-base-sentiment-latest")
|
| 368 |
+
return f"๐ฏ **Classification (fallback):** {str(result)}\n\nโ ๏ธ Note: Zero-shot with custom labels not available, showing sentiment analysis instead."
|
| 369 |
+
except:
|
| 370 |
+
return f"โ Error: {str(e)}"
|
| 371 |
+
|
| 372 |
+
def run_image_classification(image):
|
| 373 |
+
"""Image Classification function."""
|
| 374 |
+
if not CLIENT:
|
| 375 |
+
return "โ Client not available"
|
| 376 |
+
|
| 377 |
+
if image is None:
|
| 378 |
+
return "โ Please upload an image"
|
| 379 |
+
|
| 380 |
+
try:
|
| 381 |
+
logger.info(f"Image classification call: image type={type(image)}")
|
| 382 |
+
|
| 383 |
+
result = CLIENT.image_classification(image, model="google/vit-base-patch16-224")
|
| 384 |
+
|
| 385 |
+
logger.info(f"Image classification result: {type(result)}")
|
| 386 |
+
|
| 387 |
+
if isinstance(result, list):
|
| 388 |
+
output = "๐ผ๏ธ **Image Classification:**\n"
|
| 389 |
+
for i, pred in enumerate(result[:5], 1):
|
| 390 |
+
label = pred.get("label", "Unknown")
|
| 391 |
+
score = pred.get("score", 0)
|
| 392 |
+
output += f"{i}. **{label}** ({score:.1%})\n"
|
| 393 |
+
return output
|
| 394 |
+
return str(result)
|
| 395 |
+
|
| 396 |
+
except Exception as e:
|
| 397 |
+
logger.error(f"Image classification error: {e}")
|
| 398 |
+
return f"โ Error: {str(e)}"
|
| 399 |
+
|
| 400 |
+
def run_automatic_speech_recognition(audio):
|
| 401 |
+
"""Automatic Speech Recognition function."""
|
| 402 |
+
if not CLIENT:
|
| 403 |
+
return "โ Client not available"
|
| 404 |
+
|
| 405 |
+
if audio is None:
|
| 406 |
+
return "โ Please upload an audio file or record audio"
|
| 407 |
+
|
| 408 |
+
try:
|
| 409 |
+
logger.info(f"ASR call: audio type={type(audio)}")
|
| 410 |
+
|
| 411 |
+
# Handle different audio input types
|
| 412 |
+
if isinstance(audio, tuple):
|
| 413 |
+
# Convert numpy array to temporary file
|
| 414 |
+
if not SCIPY_AVAILABLE:
|
| 415 |
+
return "โ scipy is required for audio processing. Install with: pip install scipy"
|
| 416 |
+
|
| 417 |
+
import scipy.io.wavfile as wav
|
| 418 |
+
sample_rate, audio_array = audio
|
| 419 |
+
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp_file:
|
| 420 |
+
wav.write(tmp_file.name, sample_rate, audio_array)
|
| 421 |
+
audio_path = tmp_file.name
|
| 422 |
+
else:
|
| 423 |
+
audio_path = audio
|
| 424 |
+
|
| 425 |
+
result = CLIENT.automatic_speech_recognition(audio_path, model="openai/whisper-large-v3")
|
| 426 |
+
|
| 427 |
+
logger.info(f"ASR result: {type(result)}")
|
| 428 |
+
|
| 429 |
+
if isinstance(result, dict):
|
| 430 |
+
text = result.get('text', str(result))
|
| 431 |
+
return f"๐ค **Transcription:** {text}"
|
| 432 |
+
else:
|
| 433 |
+
return f"๐ค **Transcription:** {str(result)}"
|
| 434 |
+
|
| 435 |
+
except Exception as e:
|
| 436 |
+
logger.error(f"ASR error: {e}")
|
| 437 |
+
return f"โ Error: {str(e)}"
|
| 438 |
+
|
| 439 |
+
def run_text_to_image(prompt):
|
| 440 |
+
"""Text to Image function."""
|
| 441 |
+
if not CLIENT:
|
| 442 |
+
return "โ Client not available"
|
| 443 |
+
|
| 444 |
+
if not prompt or str(prompt).strip() == "":
|
| 445 |
+
return "โ Please enter a text prompt for image generation"
|
| 446 |
+
|
| 447 |
+
clean_prompt = str(prompt).strip()
|
| 448 |
+
|
| 449 |
+
try:
|
| 450 |
+
logger.info(f"Text to image call: prompt='{clean_prompt}'")
|
| 451 |
+
|
| 452 |
+
# Note: This returns a PIL Image object
|
| 453 |
+
image = CLIENT.text_to_image(clean_prompt, model="black-forest-labs/FLUX.1-schnell")
|
| 454 |
+
|
| 455 |
+
logger.info(f"Text to image result: {type(image)}")
|
| 456 |
+
|
| 457 |
+
return f"๐จ **Image Generated Successfully!** \n๐ Prompt: {clean_prompt}\n๐ผ๏ธ Check the generated image above."
|
| 458 |
+
|
| 459 |
except Exception as e:
|
| 460 |
+
logger.error(f"Text to image error: {e}")
|
| 461 |
return f"โ Error: {str(e)}"
|
| 462 |
|
| 463 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 464 |
+
# ๐จ COMPREHENSIVE GRADIO INTERFACE - ALL AI CAPABILITIES
|
| 465 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 466 |
|
| 467 |
+
# Create interface with comprehensive AI capabilities
|
| 468 |
+
with gr.Blocks(title="๐ Complete AI Research Hub") as demo:
|
| 469 |
|
| 470 |
+
gr.Markdown("# ๐ Complete AI Research Hub\n### **All HuggingFace Inference API Capabilities**")
|
| 471 |
|
| 472 |
if CLIENT:
|
| 473 |
gr.Markdown("โ
**Status:** Connected and tested successfully")
|
| 474 |
else:
|
| 475 |
gr.Markdown("โ **Status:** Set HF_API_TOKEN environment variable")
|
| 476 |
|
| 477 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 478 |
+
# ๐ฌ CHAT & TEXT GENERATION
|
| 479 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 480 |
+
|
| 481 |
gr.Markdown("## ๐ฌ Chat with AI")
|
| 482 |
chat_input = gr.Textbox(label="Your Message", placeholder="Ask anything...")
|
| 483 |
chat_output = gr.Textbox(label="AI Response", lines=5)
|
|
|
|
| 486 |
|
| 487 |
gr.Markdown("---")
|
| 488 |
|
| 489 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 490 |
+
# ๐ญ TEXT UNDERSTANDING & PREDICTION
|
| 491 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 492 |
+
|
| 493 |
+
gr.Markdown("## ๐ญ Fill Mask Prediction")
|
| 494 |
mask_input = gr.Textbox(
|
| 495 |
label="Text with [MASK]",
|
| 496 |
value="The capital of France is [MASK].",
|
|
|
|
| 502 |
|
| 503 |
gr.Markdown("---")
|
| 504 |
|
|
|
|
| 505 |
gr.Markdown("## โ Question Answering")
|
| 506 |
qa_question = gr.Textbox(label="Question", value="What is AI?")
|
| 507 |
qa_context = gr.Textbox(
|
|
|
|
| 515 |
|
| 516 |
gr.Markdown("---")
|
| 517 |
|
| 518 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 519 |
+
# ๐ TEXT PROCESSING & ANALYSIS
|
| 520 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 521 |
+
|
| 522 |
gr.Markdown("## ๐ Text Summarization")
|
| 523 |
sum_input = gr.Textbox(
|
| 524 |
label="Text to Summarize",
|
|
|
|
| 530 |
sum_btn.click(run_summarization, inputs=sum_input, outputs=sum_output)
|
| 531 |
|
| 532 |
gr.Markdown("---")
|
| 533 |
+
|
| 534 |
+
gr.Markdown("## ๐ท๏ธ Text Classification")
|
| 535 |
+
tc_input = gr.Textbox(
|
| 536 |
+
label="Text to Classify",
|
| 537 |
+
value="I love this new AI technology! It's amazing.",
|
| 538 |
+
placeholder="Enter text for sentiment/topic classification"
|
| 539 |
+
)
|
| 540 |
+
tc_output = gr.Textbox(label="Classification Results", lines=4)
|
| 541 |
+
tc_btn = gr.Button("Classify", variant="primary")
|
| 542 |
+
tc_btn.click(run_text_classification, inputs=tc_input, outputs=tc_output)
|
| 543 |
+
|
| 544 |
+
gr.Markdown("---")
|
| 545 |
+
|
| 546 |
+
gr.Markdown("## ๐ฏ Zero-Shot Classification")
|
| 547 |
+
zsc_text = gr.Textbox(
|
| 548 |
+
label="Text to Classify",
|
| 549 |
+
value="I need to return this broken phone and get my money back.",
|
| 550 |
+
placeholder="Enter text for classification"
|
| 551 |
+
)
|
| 552 |
+
zsc_labels = gr.Textbox(
|
| 553 |
+
label="Candidate Labels (comma-separated)",
|
| 554 |
+
value="refund, complaint, question, compliment",
|
| 555 |
+
placeholder="Enter possible labels separated by commas"
|
| 556 |
+
)
|
| 557 |
+
zsc_output = gr.Textbox(label="Classification Results", lines=4)
|
| 558 |
+
zsc_btn = gr.Button("Classify", variant="primary")
|
| 559 |
+
zsc_btn.click(run_zero_shot_classification, inputs=[zsc_text, zsc_labels], outputs=zsc_output)
|
| 560 |
+
|
| 561 |
+
gr.Markdown("---")
|
| 562 |
+
|
| 563 |
+
gr.Markdown("## ๐ท๏ธ Named Entity Recognition")
|
| 564 |
+
ner_input = gr.Textbox(
|
| 565 |
+
label="Text for Entity Recognition",
|
| 566 |
+
value="My name is John Smith and I work at Google in New York.",
|
| 567 |
+
placeholder="Enter text to extract named entities"
|
| 568 |
+
)
|
| 569 |
+
ner_output = gr.Textbox(label="Named Entities", lines=5)
|
| 570 |
+
ner_btn = gr.Button("Extract Entities", variant="primary")
|
| 571 |
+
ner_btn.click(run_token_classification, inputs=ner_input, outputs=ner_output)
|
| 572 |
+
|
| 573 |
+
gr.Markdown("---")
|
| 574 |
+
|
| 575 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 576 |
+
# ๐ LANGUAGE & TRANSLATION
|
| 577 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 578 |
+
|
| 579 |
+
gr.Markdown("## ๐ Language Translation")
|
| 580 |
+
trans_input = gr.Textbox(
|
| 581 |
+
label="English Text to Translate",
|
| 582 |
+
value="Hello, how are you today? I hope you have a wonderful day.",
|
| 583 |
+
placeholder="Enter English text to translate to French"
|
| 584 |
+
)
|
| 585 |
+
trans_output = gr.Textbox(label="French Translation", lines=3)
|
| 586 |
+
trans_btn = gr.Button("Translate to French", variant="primary")
|
| 587 |
+
trans_btn.click(run_translation, inputs=trans_input, outputs=trans_output)
|
| 588 |
+
|
| 589 |
+
gr.Markdown("---")
|
| 590 |
+
|
| 591 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 592 |
+
# ๐งฎ EMBEDDINGS & FEATURES
|
| 593 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ๏ฟฝ๏ฟฝโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 594 |
+
|
| 595 |
+
gr.Markdown("## ๐งฎ Feature Extraction (Embeddings)")
|
| 596 |
+
fe_input = gr.Textbox(
|
| 597 |
+
label="Text for Feature Extraction",
|
| 598 |
+
value="Machine learning is transforming the world.",
|
| 599 |
+
placeholder="Enter text to generate embeddings"
|
| 600 |
+
)
|
| 601 |
+
fe_output = gr.Textbox(label="Feature Vector Info", lines=4)
|
| 602 |
+
fe_btn = gr.Button("Extract Features", variant="primary")
|
| 603 |
+
fe_btn.click(run_feature_extraction, inputs=fe_input, outputs=fe_output)
|
| 604 |
+
|
| 605 |
+
gr.Markdown("---")
|
| 606 |
+
|
| 607 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 608 |
+
# ๐ค AUDIO PROCESSING
|
| 609 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 610 |
+
|
| 611 |
+
gr.Markdown("## ๐ค Speech Recognition")
|
| 612 |
+
asr_input = gr.Audio(
|
| 613 |
+
sources=["upload", "microphone"],
|
| 614 |
+
type="numpy",
|
| 615 |
+
label="Upload Audio or Record"
|
| 616 |
+
)
|
| 617 |
+
asr_output = gr.Textbox(label="Transcription", lines=4)
|
| 618 |
+
asr_btn = gr.Button("Transcribe Audio", variant="primary")
|
| 619 |
+
asr_btn.click(run_automatic_speech_recognition, inputs=asr_input, outputs=asr_output)
|
| 620 |
+
|
| 621 |
+
gr.Markdown("---")
|
| 622 |
+
|
| 623 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 624 |
+
# ๐ผ๏ธ IMAGE PROCESSING
|
| 625 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 626 |
+
|
| 627 |
+
gr.Markdown("## ๐ผ๏ธ Image Classification")
|
| 628 |
+
img_input = gr.Image(type="filepath", label="Upload Image")
|
| 629 |
+
img_output = gr.Textbox(label="Classification Results", lines=5)
|
| 630 |
+
img_btn = gr.Button("Classify Image", variant="primary")
|
| 631 |
+
img_btn.click(run_image_classification, inputs=img_input, outputs=img_output)
|
| 632 |
+
|
| 633 |
+
gr.Markdown("---")
|
| 634 |
+
|
| 635 |
+
gr.Markdown("## ๐จ Text to Image Generation")
|
| 636 |
+
tti_input = gr.Textbox(
|
| 637 |
+
label="Image Description",
|
| 638 |
+
value="A beautiful sunset over mountains with a lake in the foreground",
|
| 639 |
+
placeholder="Describe the image you want to generate"
|
| 640 |
+
)
|
| 641 |
+
tti_output_img = gr.Image(label="Generated Image")
|
| 642 |
+
tti_output_text = gr.Textbox(label="Generation Status", lines=2)
|
| 643 |
+
tti_btn = gr.Button("Generate Image", variant="primary")
|
| 644 |
+
|
| 645 |
+
def run_text_to_image_with_display(prompt):
|
| 646 |
+
status = run_text_to_image(prompt)
|
| 647 |
+
try:
|
| 648 |
+
if CLIENT and prompt and str(prompt).strip():
|
| 649 |
+
image = CLIENT.text_to_image(str(prompt).strip(), model="black-forest-labs/FLUX.1-schnell")
|
| 650 |
+
return image, status
|
| 651 |
+
else:
|
| 652 |
+
return None, status
|
| 653 |
+
except Exception as e:
|
| 654 |
+
return None, f"โ Error: {str(e)}"
|
| 655 |
+
|
| 656 |
+
tti_btn.click(run_text_to_image_with_display, inputs=tti_input, outputs=[tti_output_img, tti_output_text])
|
| 657 |
+
|
| 658 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 659 |
+
# ๐ SETUP & INFO
|
| 660 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 661 |
+
|
| 662 |
+
gr.Markdown("---")
|
| 663 |
+
gr.Markdown("""
|
| 664 |
+
## ๐ง Setup Instructions
|
| 665 |
+
|
| 666 |
+
**For full functionality:**
|
| 667 |
+
```bash
|
| 668 |
+
# Install dependencies
|
| 669 |
+
pip install gradio huggingface_hub scipy
|
| 670 |
+
|
| 671 |
+
# Set API token
|
| 672 |
+
export HF_API_TOKEN="your_token_here"
|
| 673 |
+
```
|
| 674 |
+
|
| 675 |
+
**Get your token:** https://huggingface.co/settings/tokens
|
| 676 |
+
|
| 677 |
+
**๐ Dependency Status:**
|
| 678 |
+
- ๐ค HuggingFace Hub: {}
|
| 679 |
+
- ๐ต Scipy (for audio): {}
|
| 680 |
+
|
| 681 |
+
**๐ฏ Available Capabilities:**
|
| 682 |
+
- ๐ฌ **Chat** - Conversational AI with Qwen-2.5-72B
|
| 683 |
+
- ๐ญ **Fill Mask** - Text completion with BERT
|
| 684 |
+
- โ **Q&A** - Question answering with RoBERTa
|
| 685 |
+
- ๐ **Summarization** - Text summarization with BART
|
| 686 |
+
- ๐ท๏ธ **Text Classification** - Sentiment analysis
|
| 687 |
+
- ๐ฏ **Zero-Shot Classification** - Custom label classification
|
| 688 |
+
- ๐ท๏ธ **NER** - Named entity recognition with BERT
|
| 689 |
+
- ๐ **Translation** - English to French with Helsinki-NLP
|
| 690 |
+
- ๐งฎ **Feature Extraction** - Text embeddings with E5
|
| 691 |
+
- ๐ค **Speech Recognition** - Audio transcription with Whisper
|
| 692 |
+
- ๐ผ๏ธ **Image Classification** - Visual analysis with ViT
|
| 693 |
+
- ๐จ **Text to Image** - AI image generation with FLUX
|
| 694 |
+
|
| 695 |
+
**๐ Perfect for research, education, and development!**
|
| 696 |
+
""".format(
|
| 697 |
+
"โ
Available" if HF_AVAILABLE else "โ Missing",
|
| 698 |
+
"โ
Available" if SCIPY_AVAILABLE else "โ ๏ธ Optional (needed for audio)"
|
| 699 |
+
))
|
| 700 |
|
| 701 |
if __name__ == "__main__":
|
| 702 |
demo.launch(
|
| 703 |
server_name="0.0.0.0",
|
| 704 |
server_port=7860,
|
| 705 |
share=False
|
| 706 |
+
)
|
| 707 |
+
|