multi-mode / app.py
vinayarni's picture
create app.py
8ab36c7 verified
import gradio as gr
from transformers import pipeline
translator = pipeline("translation_en_to_de",model="Helsinki-NLP/opus-mt-en-fr")
sa = pipeline("sentiment-analysis",model = "MarieAngeA13/Sentiment-Analysis-BERT")
text_gen = pipeline("text-generation", model="gpt2")
def translate_to_fr(text):
translate = translator(text ,max_length = len(text.split())+5)
return translate[0]['translation_text']
def generate_text(prompt):
generated_text = text_gen(prompt, max_length=len(prompt.split()) + 5, num_return_sequences=1, do_sample=True)
return generated_text[0]['generated_text']
def sentiment_analysis(text):
sentiment = sa(text)
if sentiment[0]['label'] == 'positive':
return "Happy"
if sentiment[0]['label'] == 'negative':
return "Unhappy"
if sentiment[0]['label'] == 'neutral':
return "Neither happy nor unhappy"
with gr.Blocks() as demo:
gr.Markdown("Text Pipeline :Translation, Text Generation, Sentiment Analysis")
with gr.Row():
translate_btn = gr.Button("Translate")
generate_btn = gr.Button("Generate")
analyze_btn = gr.Button("Analyze")
input_text = gr.Textbox(label="Enter text")
output_text = gr.Textbox(label="Output")
translate_btn.click(translate_to_fr, inputs=input_text, outputs=output_text)
generate_btn.click(generate_text, inputs=input_text, outputs=output_text)
analyze_btn.click(sentiment_analysis, inputs=input_text, outputs=output_text)
demo.launch()