|
|
import gradio as gr |
|
|
import os |
|
|
import json |
|
|
import logging |
|
|
import tempfile |
|
|
|
|
|
|
|
|
try: |
|
|
import scipy.io.wavfile |
|
|
SCIPY_AVAILABLE = True |
|
|
except ImportError: |
|
|
SCIPY_AVAILABLE = False |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try: |
|
|
from huggingface_hub import InferenceClient |
|
|
HF_AVAILABLE = True |
|
|
except ImportError: |
|
|
HF_AVAILABLE = False |
|
|
|
|
|
|
|
|
logging.basicConfig(level=logging.INFO) |
|
|
logger = logging.getLogger(__name__) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_client(): |
|
|
"""Get HuggingFace client - exactly like your working example.""" |
|
|
api_token = os.getenv("HF_API_TOKEN") |
|
|
|
|
|
if not HF_AVAILABLE: |
|
|
logger.error("โ HuggingFace Hub not available") |
|
|
return None |
|
|
|
|
|
if not api_token: |
|
|
logger.error("โ HF_API_TOKEN not set") |
|
|
return None |
|
|
|
|
|
try: |
|
|
logger.info("๐ Initializing HuggingFace client...") |
|
|
client = InferenceClient( |
|
|
provider="hf-inference", |
|
|
api_key=api_token, |
|
|
) |
|
|
|
|
|
|
|
|
logger.info("๐งช Testing client with fill_mask...") |
|
|
test_result = client.fill_mask("The capital of France is [MASK].", model="google-bert/bert-base-uncased") |
|
|
logger.info(f"โ
Client test successful: {type(test_result)}, got {len(test_result) if isinstance(test_result, list) else 'non-list'} results") |
|
|
|
|
|
return client |
|
|
|
|
|
except Exception as e: |
|
|
logger.error(f"โ Client initialization failed: {e}") |
|
|
return None |
|
|
|
|
|
CLIENT = get_client() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def run_chat(message): |
|
|
"""Chat function - using your exact working model.""" |
|
|
if not CLIENT: |
|
|
return "โ Client not available" |
|
|
|
|
|
|
|
|
if not message or str(message).strip() == "": |
|
|
return "โ Please enter a message" |
|
|
|
|
|
try: |
|
|
clean_message = str(message).strip() |
|
|
messages = [{"role": "user", "content": clean_message}] |
|
|
|
|
|
logger.info(f"Chat call: message='{clean_message}'") |
|
|
|
|
|
completion = CLIENT.chat.completions.create( |
|
|
model="Qwen/Qwen2.5-72B-Instruct", |
|
|
messages=messages, |
|
|
) |
|
|
|
|
|
result = completion.choices[0].message.content |
|
|
logger.info(f"Chat result: {type(result)}") |
|
|
|
|
|
return result |
|
|
|
|
|
except Exception as e: |
|
|
logger.error(f"Chat error: {e}") |
|
|
return f"โ Error: {str(e)}\n\n**Debug:** Message: '{message}'" |
|
|
|
|
|
def run_fill_mask(text): |
|
|
"""Fill mask - using your exact working approach.""" |
|
|
if not CLIENT: |
|
|
return "โ Client not available" |
|
|
|
|
|
|
|
|
if not text or str(text).strip() == "": |
|
|
return "โ Please enter text with [MASK]" |
|
|
|
|
|
clean_text = str(text).strip() |
|
|
|
|
|
if "[MASK]" not in clean_text: |
|
|
return "โ Text must contain [MASK] token" |
|
|
|
|
|
try: |
|
|
logger.info(f"Fill mask call: text='{clean_text}'") |
|
|
|
|
|
result = CLIENT.fill_mask(clean_text, model="google-bert/bert-base-uncased") |
|
|
|
|
|
logger.info(f"Fill mask result: {type(result)}") |
|
|
|
|
|
if isinstance(result, list): |
|
|
output = "๐ญ **Predictions:**\n" |
|
|
for i, pred in enumerate(result[:5], 1): |
|
|
token = pred.get("token_str", "").strip() |
|
|
score = pred.get("score", 0) |
|
|
output += f"{i}. **{token}** ({score:.3f})\n" |
|
|
return output |
|
|
return str(result) |
|
|
|
|
|
except Exception as e: |
|
|
logger.error(f"Fill mask error: {e}") |
|
|
return f"โ Error: {str(e)}\n\n**Debug:** Text: '{text}'" |
|
|
|
|
|
def run_question_answering(question, context): |
|
|
"""Q&A function - Fixed parameter handling.""" |
|
|
if not CLIENT: |
|
|
return "โ Client not available" |
|
|
|
|
|
|
|
|
if not question or not context or question.strip() == "" or context.strip() == "": |
|
|
return "โ Please provide both question and context" |
|
|
|
|
|
try: |
|
|
|
|
|
clean_question = str(question).strip() |
|
|
clean_context = str(context).strip() |
|
|
|
|
|
logger.info(f"Q&A call: question='{clean_question}', context='{clean_context[:50]}...'") |
|
|
|
|
|
answer = CLIENT.question_answering( |
|
|
question=clean_question, |
|
|
context=clean_context, |
|
|
model="deepset/roberta-base-squad2", |
|
|
) |
|
|
|
|
|
logger.info(f"Q&A result: {type(answer)}, {answer}") |
|
|
|
|
|
if isinstance(answer, dict): |
|
|
return f"๐ก **Answer:** {answer.get('answer', 'No answer found')}\n๐ **Score:** {answer.get('score', 'N/A')}" |
|
|
else: |
|
|
return f"๐ก **Answer:** {str(answer)}" |
|
|
|
|
|
except Exception as e: |
|
|
logger.error(f"Q&A error: {e}") |
|
|
return f"โ Error: {str(e)}\n\n**Debug:** Question: '{question}', Context: '{context[:50] if context else 'None'}...'" |
|
|
|
|
|
def run_summarization(text): |
|
|
"""Summarization function - Fixed parameter handling.""" |
|
|
if not CLIENT: |
|
|
return "โ Client not available" |
|
|
|
|
|
|
|
|
if not text or str(text).strip() == "": |
|
|
return "โ Please enter text to summarize" |
|
|
|
|
|
clean_text = str(text).strip() |
|
|
|
|
|
if len(clean_text.split()) < 10: |
|
|
return "โ Text must be at least 10 words long" |
|
|
|
|
|
try: |
|
|
logger.info(f"Summarization call: text length={len(clean_text)}") |
|
|
|
|
|
result = CLIENT.summarization(clean_text, model="facebook/bart-large-cnn") |
|
|
|
|
|
logger.info(f"Summarization result: {type(result)}") |
|
|
|
|
|
if isinstance(result, list) and result: |
|
|
summary = result[0].get('summary_text', str(result[0])) |
|
|
return f"๐ **Summary:** {summary}" |
|
|
elif isinstance(result, dict): |
|
|
summary = result.get('summary_text', str(result)) |
|
|
return f"๐ **Summary:** {summary}" |
|
|
else: |
|
|
return f"๐ **Summary:** {str(result)}" |
|
|
|
|
|
except Exception as e: |
|
|
logger.error(f"Summarization error: {e}") |
|
|
return f"โ Error: {str(e)}\n\n**Debug:** Text length: {len(text) if text else 0}" |
|
|
|
|
|
def run_text_classification(text): |
|
|
"""Text Classification function.""" |
|
|
if not CLIENT: |
|
|
return "โ Client not available" |
|
|
|
|
|
if not text or str(text).strip() == "": |
|
|
return "โ Please enter text to classify" |
|
|
|
|
|
clean_text = str(text).strip() |
|
|
|
|
|
try: |
|
|
logger.info(f"Text classification call: text='{clean_text[:50]}...'") |
|
|
|
|
|
result = CLIENT.text_classification(clean_text, model="cardiffnlp/twitter-roberta-base-sentiment-latest") |
|
|
|
|
|
logger.info(f"Text classification result: {type(result)}") |
|
|
|
|
|
if isinstance(result, list): |
|
|
output = "๐ท๏ธ **Classifications:**\n" |
|
|
for i, pred in enumerate(result[:5], 1): |
|
|
label = pred.get("label", "Unknown") |
|
|
score = pred.get("score", 0) |
|
|
output += f"{i}. **{label}** ({score:.3f})\n" |
|
|
return output |
|
|
return str(result) |
|
|
|
|
|
except Exception as e: |
|
|
logger.error(f"Text classification error: {e}") |
|
|
return f"โ Error: {str(e)}" |
|
|
|
|
|
def run_token_classification(text): |
|
|
"""Token Classification (NER) function.""" |
|
|
if not CLIENT: |
|
|
return "โ Client not available" |
|
|
|
|
|
if not text or str(text).strip() == "": |
|
|
return "โ Please enter text for entity recognition" |
|
|
|
|
|
clean_text = str(text).strip() |
|
|
|
|
|
try: |
|
|
logger.info(f"Token classification call: text='{clean_text}'") |
|
|
|
|
|
result = CLIENT.token_classification(clean_text, model="dslim/bert-base-NER") |
|
|
|
|
|
logger.info(f"Token classification result: {type(result)}") |
|
|
|
|
|
if isinstance(result, list): |
|
|
output = "๐ท๏ธ **Named Entities:**\n" |
|
|
for i, entity in enumerate(result[:10], 1): |
|
|
word = entity.get("word", "Unknown") |
|
|
label = entity.get("entity", "Unknown") |
|
|
score = entity.get("score", 0) |
|
|
output += f"{i}. **{word}** โ {label} ({score:.3f})\n" |
|
|
return output |
|
|
return str(result) |
|
|
|
|
|
except Exception as e: |
|
|
logger.error(f"Token classification error: {e}") |
|
|
return f"โ Error: {str(e)}" |
|
|
|
|
|
def run_translation(text): |
|
|
"""Translation function.""" |
|
|
if not CLIENT: |
|
|
return "โ Client not available" |
|
|
|
|
|
if not text or str(text).strip() == "": |
|
|
return "โ Please enter text to translate" |
|
|
|
|
|
clean_text = str(text).strip() |
|
|
|
|
|
try: |
|
|
logger.info(f"Translation call: text='{clean_text[:50]}...'") |
|
|
|
|
|
result = CLIENT.translation(clean_text, model="Helsinki-NLP/opus-mt-en-fr") |
|
|
|
|
|
logger.info(f"Translation result: {type(result)}") |
|
|
|
|
|
if isinstance(result, list) and result: |
|
|
translation = result[0].get('translation_text', str(result[0])) |
|
|
return f"๐ **Translation (ENโFR):** {translation}" |
|
|
elif isinstance(result, dict): |
|
|
translation = result.get('translation_text', str(result)) |
|
|
return f"๐ **Translation (ENโFR):** {translation}" |
|
|
else: |
|
|
return f"๐ **Translation:** {str(result)}" |
|
|
|
|
|
except Exception as e: |
|
|
logger.error(f"Translation error: {e}") |
|
|
return f"โ Error: {str(e)}" |
|
|
|
|
|
def run_feature_extraction(text): |
|
|
"""Feature Extraction function.""" |
|
|
if not CLIENT: |
|
|
return "โ Client not available" |
|
|
|
|
|
if not text or str(text).strip() == "": |
|
|
return "โ Please enter text for feature extraction" |
|
|
|
|
|
clean_text = str(text).strip() |
|
|
|
|
|
try: |
|
|
logger.info(f"Feature extraction call: text='{clean_text[:50]}...'") |
|
|
|
|
|
result = CLIENT.feature_extraction(clean_text, model="intfloat/multilingual-e5-large-instruct") |
|
|
|
|
|
logger.info(f"Feature extraction result: {type(result)}") |
|
|
|
|
|
if isinstance(result, list) and result: |
|
|
dim = len(result[0]) if result[0] else 0 |
|
|
sample = result[0][:5] if dim >= 5 else result[0] |
|
|
return f"๐งฎ **Feature Vector:** Dimension: {dim}\n**Sample values:** {sample}..." |
|
|
else: |
|
|
return f"๐งฎ **Features:** {str(result)[:200]}..." |
|
|
|
|
|
except Exception as e: |
|
|
logger.error(f"Feature extraction error: {e}") |
|
|
return f"โ Error: {str(e)}" |
|
|
|
|
|
def run_zero_shot_classification(text, labels): |
|
|
"""Zero Shot Classification function.""" |
|
|
if not CLIENT: |
|
|
return "โ Client not available" |
|
|
|
|
|
if not text or str(text).strip() == "": |
|
|
return "โ Please enter text to classify" |
|
|
|
|
|
if not labels or str(labels).strip() == "": |
|
|
return "โ Please enter candidate labels (comma-separated)" |
|
|
|
|
|
clean_text = str(text).strip() |
|
|
clean_labels = [label.strip() for label in str(labels).split(",") if label.strip()] |
|
|
|
|
|
if not clean_labels: |
|
|
return "โ Please provide valid labels separated by commas" |
|
|
|
|
|
try: |
|
|
logger.info(f"Zero shot classification call: text='{clean_text[:50]}...', labels={clean_labels}") |
|
|
|
|
|
|
|
|
result = CLIENT.text_classification( |
|
|
clean_text, |
|
|
model="facebook/bart-large-mnli", |
|
|
|
|
|
) |
|
|
|
|
|
logger.info(f"Zero shot classification result: {type(result)}") |
|
|
|
|
|
if isinstance(result, list): |
|
|
output = "๐ฏ **Zero-Shot Classification:**\n" |
|
|
for i, pred in enumerate(result[:5], 1): |
|
|
label = pred.get("label", "Unknown") |
|
|
score = pred.get("score", 0) |
|
|
output += f"{i}. **{label}** ({score:.3f})\n" |
|
|
return output |
|
|
elif isinstance(result, dict): |
|
|
labels_result = result.get('labels', []) |
|
|
scores = result.get('scores', []) |
|
|
output = "๐ฏ **Zero-Shot Classification:**\n" |
|
|
for i, (label, score) in enumerate(zip(labels_result[:5], scores[:5]), 1): |
|
|
output += f"{i}. **{label}** ({score:.3f})\n" |
|
|
return output |
|
|
else: |
|
|
return f"๐ฏ **Result:** {str(result)}" |
|
|
|
|
|
except Exception as e: |
|
|
logger.error(f"Zero shot classification error: {e}") |
|
|
|
|
|
try: |
|
|
result = CLIENT.text_classification(clean_text, model="cardiffnlp/twitter-roberta-base-sentiment-latest") |
|
|
return f"๐ฏ **Classification (fallback):** {str(result)}\n\nโ ๏ธ Note: Zero-shot with custom labels not available, showing sentiment analysis instead." |
|
|
except: |
|
|
return f"โ Error: {str(e)}" |
|
|
|
|
|
def run_image_classification(image): |
|
|
"""Image Classification function.""" |
|
|
if not CLIENT: |
|
|
return "โ Client not available" |
|
|
|
|
|
if image is None: |
|
|
return "โ Please upload an image" |
|
|
|
|
|
try: |
|
|
logger.info(f"Image classification call: image type={type(image)}") |
|
|
|
|
|
result = CLIENT.image_classification(image, model="google/vit-base-patch16-224") |
|
|
|
|
|
logger.info(f"Image classification result: {type(result)}") |
|
|
|
|
|
if isinstance(result, list): |
|
|
output = "๐ผ๏ธ **Image Classification:**\n" |
|
|
for i, pred in enumerate(result[:5], 1): |
|
|
label = pred.get("label", "Unknown") |
|
|
score = pred.get("score", 0) |
|
|
output += f"{i}. **{label}** ({score:.1%})\n" |
|
|
return output |
|
|
return str(result) |
|
|
|
|
|
except Exception as e: |
|
|
logger.error(f"Image classification error: {e}") |
|
|
return f"โ Error: {str(e)}" |
|
|
|
|
|
def run_automatic_speech_recognition(audio): |
|
|
"""Automatic Speech Recognition function.""" |
|
|
if not CLIENT: |
|
|
return "โ Client not available" |
|
|
|
|
|
if audio is None: |
|
|
return "โ Please upload an audio file or record audio" |
|
|
|
|
|
try: |
|
|
logger.info(f"ASR call: audio type={type(audio)}") |
|
|
|
|
|
|
|
|
if isinstance(audio, tuple): |
|
|
|
|
|
if not SCIPY_AVAILABLE: |
|
|
return "โ scipy is required for audio processing. Install with: pip install scipy" |
|
|
|
|
|
import scipy.io.wavfile as wav |
|
|
sample_rate, audio_array = audio |
|
|
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp_file: |
|
|
wav.write(tmp_file.name, sample_rate, audio_array) |
|
|
audio_path = tmp_file.name |
|
|
else: |
|
|
audio_path = audio |
|
|
|
|
|
result = CLIENT.automatic_speech_recognition(audio_path, model="openai/whisper-large-v3") |
|
|
|
|
|
logger.info(f"ASR result: {type(result)}") |
|
|
|
|
|
if isinstance(result, dict): |
|
|
text = result.get('text', str(result)) |
|
|
return f"๐ค **Transcription:** {text}" |
|
|
else: |
|
|
return f"๐ค **Transcription:** {str(result)}" |
|
|
|
|
|
except Exception as e: |
|
|
logger.error(f"ASR error: {e}") |
|
|
return f"โ Error: {str(e)}" |
|
|
|
|
|
def run_text_to_image(prompt): |
|
|
"""Text to Image function.""" |
|
|
if not CLIENT: |
|
|
return "โ Client not available" |
|
|
|
|
|
if not prompt or str(prompt).strip() == "": |
|
|
return "โ Please enter a text prompt for image generation" |
|
|
|
|
|
clean_prompt = str(prompt).strip() |
|
|
|
|
|
try: |
|
|
logger.info(f"Text to image call: prompt='{clean_prompt}'") |
|
|
|
|
|
|
|
|
image = CLIENT.text_to_image(clean_prompt, model="black-forest-labs/FLUX.1-schnell") |
|
|
|
|
|
logger.info(f"Text to image result: {type(image)}") |
|
|
|
|
|
return f"๐จ **Image Generated Successfully!** \n๐ Prompt: {clean_prompt}\n๐ผ๏ธ Check the generated image above." |
|
|
|
|
|
except Exception as e: |
|
|
logger.error(f"Text to image error: {e}") |
|
|
return f"โ Error: {str(e)}" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
with gr.Blocks(title="๐ Complete AI Research Hub") as demo: |
|
|
|
|
|
gr.Markdown("# ๐ Complete AI Research Hub\n### **All HuggingFace Inference API Capabilities**") |
|
|
|
|
|
if CLIENT: |
|
|
gr.Markdown("โ
**Status:** Connected and tested successfully") |
|
|
else: |
|
|
gr.Markdown("โ **Status:** Set HF_API_TOKEN environment variable") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
gr.Markdown("## ๐ฌ Chat with AI") |
|
|
chat_input = gr.Textbox(label="Your Message", placeholder="Ask anything...") |
|
|
chat_output = gr.Textbox(label="AI Response", lines=5) |
|
|
chat_btn = gr.Button("Send", variant="primary") |
|
|
chat_btn.click(run_chat, inputs=chat_input, outputs=chat_output) |
|
|
|
|
|
gr.Markdown("---") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
gr.Markdown("## ๐ญ Fill Mask Prediction") |
|
|
mask_input = gr.Textbox( |
|
|
label="Text with [MASK]", |
|
|
value="The capital of France is [MASK].", |
|
|
placeholder="Enter text with [MASK] token" |
|
|
) |
|
|
mask_output = gr.Textbox(label="Predictions", lines=6) |
|
|
mask_btn = gr.Button("Predict", variant="primary") |
|
|
mask_btn.click(run_fill_mask, inputs=mask_input, outputs=mask_output) |
|
|
|
|
|
gr.Markdown("---") |
|
|
|
|
|
gr.Markdown("## โ Question Answering") |
|
|
qa_question = gr.Textbox(label="Question", value="What is AI?") |
|
|
qa_context = gr.Textbox( |
|
|
label="Context", |
|
|
lines=3, |
|
|
value="Artificial Intelligence (AI) is the simulation of human intelligence in machines." |
|
|
) |
|
|
qa_output = gr.Textbox(label="Answer", lines=3) |
|
|
qa_btn = gr.Button("Answer", variant="primary") |
|
|
qa_btn.click(run_question_answering, inputs=[qa_question, qa_context], outputs=qa_output) |
|
|
|
|
|
gr.Markdown("---") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
gr.Markdown("## ๐ Text Summarization") |
|
|
sum_input = gr.Textbox( |
|
|
label="Text to Summarize", |
|
|
lines=5, |
|
|
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. The process involves feeding data into algorithms that learn patterns and make predictions or decisions without being explicitly programmed for each specific task." |
|
|
) |
|
|
sum_output = gr.Textbox(label="Summary", lines=3) |
|
|
sum_btn = gr.Button("Summarize", variant="primary") |
|
|
sum_btn.click(run_summarization, inputs=sum_input, outputs=sum_output) |
|
|
|
|
|
gr.Markdown("---") |
|
|
|
|
|
gr.Markdown("## ๐ท๏ธ Text Classification") |
|
|
tc_input = gr.Textbox( |
|
|
label="Text to Classify", |
|
|
value="I love this new AI technology! It's amazing.", |
|
|
placeholder="Enter text for sentiment/topic classification" |
|
|
) |
|
|
tc_output = gr.Textbox(label="Classification Results", lines=4) |
|
|
tc_btn = gr.Button("Classify", variant="primary") |
|
|
tc_btn.click(run_text_classification, inputs=tc_input, outputs=tc_output) |
|
|
|
|
|
gr.Markdown("---") |
|
|
|
|
|
gr.Markdown("## ๐ฏ Zero-Shot Classification") |
|
|
zsc_text = gr.Textbox( |
|
|
label="Text to Classify", |
|
|
value="I need to return this broken phone and get my money back.", |
|
|
placeholder="Enter text for classification" |
|
|
) |
|
|
zsc_labels = gr.Textbox( |
|
|
label="Candidate Labels (comma-separated)", |
|
|
value="refund, complaint, question, compliment", |
|
|
placeholder="Enter possible labels separated by commas" |
|
|
) |
|
|
zsc_output = gr.Textbox(label="Classification Results", lines=4) |
|
|
zsc_btn = gr.Button("Classify", variant="primary") |
|
|
zsc_btn.click(run_zero_shot_classification, inputs=[zsc_text, zsc_labels], outputs=zsc_output) |
|
|
|
|
|
gr.Markdown("---") |
|
|
|
|
|
gr.Markdown("## ๐ท๏ธ Named Entity Recognition") |
|
|
ner_input = gr.Textbox( |
|
|
label="Text for Entity Recognition", |
|
|
value="My name is John Smith and I work at Google in New York.", |
|
|
placeholder="Enter text to extract named entities" |
|
|
) |
|
|
ner_output = gr.Textbox(label="Named Entities", lines=5) |
|
|
ner_btn = gr.Button("Extract Entities", variant="primary") |
|
|
ner_btn.click(run_token_classification, inputs=ner_input, outputs=ner_output) |
|
|
|
|
|
gr.Markdown("---") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
gr.Markdown("## ๐ Language Translation") |
|
|
trans_input = gr.Textbox( |
|
|
label="English Text to Translate", |
|
|
value="Hello, how are you today? I hope you have a wonderful day.", |
|
|
placeholder="Enter English text to translate to French" |
|
|
) |
|
|
trans_output = gr.Textbox(label="French Translation", lines=3) |
|
|
trans_btn = gr.Button("Translate to French", variant="primary") |
|
|
trans_btn.click(run_translation, inputs=trans_input, outputs=trans_output) |
|
|
|
|
|
gr.Markdown("---") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
gr.Markdown("## ๐งฎ Feature Extraction (Embeddings)") |
|
|
fe_input = gr.Textbox( |
|
|
label="Text for Feature Extraction", |
|
|
value="Machine learning is transforming the world.", |
|
|
placeholder="Enter text to generate embeddings" |
|
|
) |
|
|
fe_output = gr.Textbox(label="Feature Vector Info", lines=4) |
|
|
fe_btn = gr.Button("Extract Features", variant="primary") |
|
|
fe_btn.click(run_feature_extraction, inputs=fe_input, outputs=fe_output) |
|
|
|
|
|
gr.Markdown("---") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
gr.Markdown("## ๐ค Speech Recognition") |
|
|
asr_input = gr.Audio( |
|
|
sources=["upload", "microphone"], |
|
|
type="numpy", |
|
|
label="Upload Audio or Record" |
|
|
) |
|
|
asr_output = gr.Textbox(label="Transcription", lines=4) |
|
|
asr_btn = gr.Button("Transcribe Audio", variant="primary") |
|
|
asr_btn.click(run_automatic_speech_recognition, inputs=asr_input, outputs=asr_output) |
|
|
|
|
|
gr.Markdown("---") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
gr.Markdown("## ๐ผ๏ธ Image Classification") |
|
|
img_input = gr.Image(type="filepath", label="Upload Image") |
|
|
img_output = gr.Textbox(label="Classification Results", lines=5) |
|
|
img_btn = gr.Button("Classify Image", variant="primary") |
|
|
img_btn.click(run_image_classification, inputs=img_input, outputs=img_output) |
|
|
|
|
|
gr.Markdown("---") |
|
|
|
|
|
gr.Markdown("## ๐จ Text to Image Generation") |
|
|
tti_input = gr.Textbox( |
|
|
label="Image Description", |
|
|
value="A beautiful sunset over mountains with a lake in the foreground", |
|
|
placeholder="Describe the image you want to generate" |
|
|
) |
|
|
tti_output_img = gr.Image(label="Generated Image") |
|
|
tti_output_text = gr.Textbox(label="Generation Status", lines=2) |
|
|
tti_btn = gr.Button("Generate Image", variant="primary") |
|
|
|
|
|
def run_text_to_image_with_display(prompt): |
|
|
status = run_text_to_image(prompt) |
|
|
try: |
|
|
if CLIENT and prompt and str(prompt).strip(): |
|
|
image = CLIENT.text_to_image(str(prompt).strip(), model="black-forest-labs/FLUX.1-schnell") |
|
|
return image, status |
|
|
else: |
|
|
return None, status |
|
|
except Exception as e: |
|
|
return None, f"โ Error: {str(e)}" |
|
|
|
|
|
tti_btn.click(run_text_to_image_with_display, inputs=tti_input, outputs=[tti_output_img, tti_output_text]) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
gr.Markdown("---") |
|
|
gr.Markdown(""" |
|
|
## ๐ง Setup Instructions |
|
|
|
|
|
**For full functionality:** |
|
|
```bash |
|
|
# Install dependencies |
|
|
pip install gradio huggingface_hub scipy |
|
|
|
|
|
# Set API token |
|
|
export HF_API_TOKEN="your_token_here" |
|
|
``` |
|
|
|
|
|
**Get your token:** https://huggingface.co/settings/tokens |
|
|
|
|
|
**๐ Dependency Status:** |
|
|
- ๐ค HuggingFace Hub: {} |
|
|
- ๐ต Scipy (for audio): {} |
|
|
|
|
|
**๐ฏ Available Capabilities:** |
|
|
- ๐ฌ **Chat** - Conversational AI with Qwen-2.5-72B |
|
|
- ๐ญ **Fill Mask** - Text completion with BERT |
|
|
- โ **Q&A** - Question answering with RoBERTa |
|
|
- ๐ **Summarization** - Text summarization with BART |
|
|
- ๐ท๏ธ **Text Classification** - Sentiment analysis |
|
|
- ๐ฏ **Zero-Shot Classification** - Custom label classification |
|
|
- ๐ท๏ธ **NER** - Named entity recognition with BERT |
|
|
- ๐ **Translation** - English to French with Helsinki-NLP |
|
|
- ๐งฎ **Feature Extraction** - Text embeddings with E5 |
|
|
- ๐ค **Speech Recognition** - Audio transcription with Whisper |
|
|
- ๐ผ๏ธ **Image Classification** - Visual analysis with ViT |
|
|
- ๐จ **Text to Image** - AI image generation with FLUX |
|
|
|
|
|
**๐ Perfect for research, education, and development!** |
|
|
""".format( |
|
|
"โ
Available" if HF_AVAILABLE else "โ Missing", |
|
|
"โ
Available" if SCIPY_AVAILABLE else "โ ๏ธ Optional (needed for audio)" |
|
|
)) |
|
|
|
|
|
if __name__ == "__main__": |
|
|
demo.launch( |
|
|
server_name="0.0.0.0", |
|
|
server_port=7860, |
|
|
share=False |
|
|
) |
|
|
|