Spaces:
Sleeping
Sleeping
create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
translator = pipeline("translation_en_to_de",model="Helsinki-NLP/opus-mt-en-fr")
|
4 |
+
sa = pipeline("sentiment-analysis",model = "MarieAngeA13/Sentiment-Analysis-BERT")
|
5 |
+
text_gen = pipeline("text-generation", model="gpt2")
|
6 |
+
def translate_to_fr(text):
|
7 |
+
translate = translator(text ,max_length = len(text.split())+5)
|
8 |
+
return translate[0]['translation_text']
|
9 |
+
def generate_text(prompt):
|
10 |
+
generated_text = text_gen(prompt, max_length=len(prompt.split()) + 5, num_return_sequences=1, do_sample=True)
|
11 |
+
return generated_text[0]['generated_text']
|
12 |
+
def sentiment_analysis(text):
|
13 |
+
sentiment = sa(text)
|
14 |
+
if sentiment[0]['label'] == 'positive':
|
15 |
+
return "Happy"
|
16 |
+
if sentiment[0]['label'] == 'negative':
|
17 |
+
return "Unhappy"
|
18 |
+
if sentiment[0]['label'] == 'neutral':
|
19 |
+
return "Neither happy nor unhappy"
|
20 |
+
with gr.Blocks() as demo:
|
21 |
+
gr.Markdown("Text Pipeline :Translation, Text Generation, Sentiment Analysis")
|
22 |
+
|
23 |
+
with gr.Row():
|
24 |
+
translate_btn = gr.Button("Translate")
|
25 |
+
generate_btn = gr.Button("Generate")
|
26 |
+
analyze_btn = gr.Button("Analyze")
|
27 |
+
|
28 |
+
input_text = gr.Textbox(label="Enter text")
|
29 |
+
output_text = gr.Textbox(label="Output")
|
30 |
+
|
31 |
+
translate_btn.click(translate_to_fr, inputs=input_text, outputs=output_text)
|
32 |
+
generate_btn.click(generate_text, inputs=input_text, outputs=output_text)
|
33 |
+
analyze_btn.click(sentiment_analysis, inputs=input_text, outputs=output_text)
|
34 |
+
demo.launch()
|