Spaces:
Sleeping
Sleeping
File size: 2,777 Bytes
0534365 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
import gradio as gr
from transformers import pipeline
# --------- Load Models ---------
sentiment_model = pipeline("sentiment-analysis")
summarizer = pipeline("summarization")
qa_model = pipeline("question-answering")
translator = pipeline("translation_en_to_fr")
zero_shot = pipeline("zero-shot-classification")
# --------- Pipeline Functions ---------
def run_app(task, text, context="", labels=""):
if task == "Sentiment Analysis":
result = sentiment_model(text)[0]
return f"{result['label']} (confidence: {round(result['score'], 3)})"
elif task == "Text Summarization":
result = summarizer(
text,
max_length=120,
min_length=30,
do_sample=False
)[0]
return result["summary_text"]
elif task == "Question Answering":
if not context:
return "Please provide a context passage."
result = qa_model(question=text, context=context)
return result["answer"]
elif task == "English → French Translation":
result = translator(text)[0]
return result["translation_text"]
elif task == "Zero-Shot Classification":
if not labels:
return "Please enter candidate labels (comma-separated)."
label_list = [x.strip() for x in labels.split(",")]
result = zero_shot(text, candidate_labels=label_list)
lines = [
f"{label}: {round(score, 3)}"
for label, score in zip(result["labels"], result["scores"])
]
return "\n".join(lines)
return "Select a valid task."
# --------- Gradio UI ---------
with gr.Blocks(title="Hugging Face AI Playground") as demo:
gr.Markdown(
"## 🤗 Hugging Face AI App\n"
"Select a task and run inference using pretrained Transformer models."
)
task = gr.Dropdown(
choices=[
"Sentiment Analysis",
"Text Summarization",
"Question Answering",
"English → French Translation",
"Zero-Shot Classification",
],
value="Sentiment Analysis",
label="Choose a Task"
)
text = gr.Textbox(lines=4, label="Input Text")
context = gr.Textbox(
lines=4,
label="Context (only for Question Answering)",
visible=True
)
labels = gr.Textbox(
label="Candidate Labels (comma-separated, for Zero-Shot Classification)"
)
output = gr.Textbox(label="Model Output")
run_button = gr.Button("Run")
run_button.click(
fn=run_app,
inputs=[task, text, context, labels],
outputs=output
)
demo.launch()
|