Spaces:
Sleeping
Sleeping
File size: 5,662 Bytes
767d0fb f8d9055 767d0fb 77d05cb c4f352b 767d0fb f8d9055 767d0fb f8d9055 77d05cb f8d9055 77d05cb f8d9055 767d0fb c4f352b 77d05cb 767d0fb c4f352b 767d0fb c4f352b 77d05cb c4f352b 77d05cb f8d9055 77d05cb c4f352b 77d05cb f8d9055 77d05cb c4f352b 767d0fb | 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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
import gradio as gr
from transformers import pipeline
# Load pipelines with small, CPU-friendly models
summarizer = pipeline("summarization", model="t5-small")
qa = pipeline("question-answering", model="distilbert-base-cased-distilled-squad")
sentiment = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
translator_en_id = pipeline("translation_en_to_id", model="Helsinki-NLP/opus-mt-en-id")
translator_id_en = pipeline("translation_id_to_en", model="Helsinki-NLP/opus-mt-id-en")
ner = pipeline("ner", grouped_entities=True)
# New: Text Generation pipeline
text_generator = pipeline("text-generation", model="gpt2")
# Functions for each feature
def summarize_text(text):
if not text.strip():
return "Please enter text to summarize."
summary = summarizer(text, max_length=100, min_length=25, do_sample=False)
return summary[0]['summary_text']
def answer_question(context, question):
if not context.strip() or not question.strip():
return "Please provide both context and a question."
result = qa(question=question, context=context)
return result['answer']
def analyze_sentiment(text):
if not text.strip():
return "Please enter text for sentiment analysis."
result = sentiment(text)
label = result[0]['label']
score = result[0]['score']
return f"Sentiment: {label} (confidence: {score:.2f})"
def translate_en_to_id(text):
if not text.strip():
return "Please enter English text to translate."
translation = translator_en_id(text)
return translation[0]['translation_text']
def translate_id_to_en(text):
if not text.strip():
return "Please enter Malay text to translate."
translation = translator_id_en(text)
return translation[0]['translation_text']
def extract_entities(text):
if not text.strip():
return "Please enter text for entity recognition."
entities = ner(text)
if not entities:
return "No entities found."
formatted = "\n".join([f"{e['entity_group']}: {e['word']}" for e in entities])
return formatted
# New: Text Generation function
def generate_text(prompt):
if not prompt.strip():
return "Please enter a starting phrase."
result = text_generator(prompt, max_length=100, num_return_sequences=1)
return result[0]["generated_text"]
# Build Gradio interface
with gr.Blocks() as demo:
gr.Markdown("# Multi-Function AI Assistant")
gr.Markdown(
"This app provides **Text Summarization**, **Question Answering**, **Sentiment Analysis**, "
"**Translation**, **Named Entity Recognition**, and **Text Generation**. "
"All models run efficiently on CPU, suitable for free tier deployment."
)
with gr.Tab("Summarization"):
summ_input = gr.Textbox(label="Enter text to summarize", lines=5, placeholder="Paste your text here...")
summ_output = gr.Textbox(label="Summary", lines=3)
summ_button = gr.Button("Summarize")
summ_button.click(summarize_text, inputs=summ_input, outputs=summ_output)
with gr.Tab("Question Answering"):
qa_context = gr.Textbox(label="Context", lines=5, placeholder="Enter context text here...")
qa_question = gr.Textbox(label="Question", placeholder="Enter your question here...")
qa_answer = gr.Textbox(label="Answer", lines=2)
qa_button = gr.Button("Get Answer")
qa_button.click(answer_question, inputs=[qa_context, qa_question], outputs=qa_answer)
with gr.Tab("Sentiment Analysis"):
sent_input = gr.Textbox(label="Enter text for sentiment analysis", lines=4, placeholder="Type text here...")
sent_output = gr.Textbox(label="Sentiment Result")
sent_button = gr.Button("Analyze Sentiment")
sent_button.click(analyze_sentiment, inputs=sent_input, outputs=sent_output)
with gr.Tab("Named Entity Recognition"):
ner_input = gr.Textbox(label="Enter text for entity recognition", lines=5, placeholder="Type text here...")
ner_output = gr.Textbox(label="Entities Found", lines=6)
ner_button = gr.Button("Extract Entities")
ner_button.click(extract_entities, inputs=ner_input, outputs=ner_output)
with gr.Tab("Translation (EN → ID)"):
trans_input = gr.Textbox(label="Enter English text", lines=4, placeholder="Type English text here...")
trans_output = gr.Textbox(label="Indonesian Translation", lines=3)
trans_button = gr.Button("Translate")
trans_button.click(translate_en_to_id, inputs=trans_input, outputs=trans_output)
with gr.Tab("Translation (ID → EN)"):
trans_input = gr.Textbox(label="Enter Indonesian text", lines=4, placeholder="Type Indonesian text here...")
trans_output = gr.Textbox(label="English Translation", lines=3)
trans_button = gr.Button("Translate")
trans_button.click(translate_id_to_en, inputs=trans_input, outputs=trans_output)
# New: Text Generation Tab
with gr.Tab("Text Generation"):
gen_input = gr.Textbox(label="Enter a starting phrase", lines=3, placeholder="e.g., Once upon a time")
gen_output = gr.Textbox(label="Generated Text", lines=6)
gen_button = gr.Button("Generate")
gen_button.click(generate_text, inputs=gen_input, outputs=gen_output)
gr.Markdown(
"### Notes:\n"
"- Input text length is limited for performance.\n"
"- All models are CPU-optimized for free tier deployment.\n"
"- Refresh the page to reset the app.\n"
"- Feel free to explore each tab for different AI functionalities."
)
if __name__ == "__main__":
demo.launch()
|