File size: 1,433 Bytes
8ab36c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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()