Spaces:
Sleeping
Sleeping
Khaledmd12
commited on
Upload creating_a_demo.py
Browse files- creating_a_demo.py +57 -0
creating_a_demo.py
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
|
3 |
+
!pip install gradio --quiet
|
4 |
+
!pip install transformers --quiet
|
5 |
+
|
6 |
+
"""<h1> Sentiment Analysis"""
|
7 |
+
|
8 |
+
def get_sentiment(text):
|
9 |
+
return sentiment(text)[0]['label'], sentiment(text)[0]['score']
|
10 |
+
|
11 |
+
from transformers import pipeline
|
12 |
+
import gradio as gr
|
13 |
+
|
14 |
+
sentiment = pipeline("sentiment-analysis", model='distilbert/distilbert-base-uncased-finetuned-sst-2-english')
|
15 |
+
sentiment_analysis = gr.Interface(fn=get_sentiment,
|
16 |
+
inputs=gr.Textbox(lines=1, label="Enter the review:"),
|
17 |
+
outputs=[gr.Text(label='Sentiment:'),
|
18 |
+
gr.Text(label='Score:')])
|
19 |
+
|
20 |
+
"""<h1> Summarization"""
|
21 |
+
|
22 |
+
def get_summary(text):
|
23 |
+
return summarizer(text, max_length=130, min_length=30, do_sample=False)[0]['summary_text']
|
24 |
+
|
25 |
+
summarizer = pipeline("summarization", model='sshleifer/distilbart-cnn-12-6')
|
26 |
+
summarization = gr.Interface(fn=get_summary,
|
27 |
+
inputs=gr.Textbox(lines=1, label="Enter the text:"),
|
28 |
+
outputs=gr.Text(label="Summary:"))
|
29 |
+
|
30 |
+
"""<h1> Speech To Text"""
|
31 |
+
|
32 |
+
def speechToText(audio):
|
33 |
+
extractTtext = pipeline(model='openai/whisper-tiny')
|
34 |
+
return extractTtext(audio)['text']
|
35 |
+
|
36 |
+
speech_to_text = gr.Interface(fn=speechToText,
|
37 |
+
inputs=gr.Audio(sources="upload", type="filepath"),
|
38 |
+
outputs="text")
|
39 |
+
|
40 |
+
"""<h1> ChatBot"""
|
41 |
+
|
42 |
+
def get_chatbot(text):
|
43 |
+
|
44 |
+
pipe = pipeline("text-generation", model="distilbert/distilgpt2")
|
45 |
+
response = pipe(text)
|
46 |
+
return response[0]['generated_text']
|
47 |
+
|
48 |
+
chatbot = gr.Interface(fn=get_chatbot,
|
49 |
+
inputs=gr.Textbox(lines=1, label="Enter the text:"),
|
50 |
+
outputs=gr.Text(label="Response:"))
|
51 |
+
|
52 |
+
"""<h1> Creating the Tabbed Interface"""
|
53 |
+
|
54 |
+
demo = gr.TabbedInterface([sentiment_analysis, summarization, speech_to_text, chatbot], ["Sentiment Analysis", "Summarization", 'Speech to Text', 'Chat Bot'])
|
55 |
+
|
56 |
+
if __name__ == "__main__":
|
57 |
+
demo.launch()
|