AbdulrahmanJ commited on
Commit
be98f23
·
verified ·
1 Parent(s): 64170b7

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -0
app.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+
4
+ # Load pre-trained models
5
+ sentiment_model = pipeline("sentiment-analysis")
6
+ chatbot_model = pipeline("text-generation", model="microsoft/DialoGPT-medium")
7
+ summarization_model = pipeline("summarization")
8
+ text_to_speech_model = pipeline("text-to-speech")
9
+
10
+ # Sentiment Analysis Function
11
+ def analyze_sentiment(text):
12
+ result = sentiment_model(text)[0]
13
+ return result["label"], round(result["score"], 4)
14
+
15
+ # Chatbot Function
16
+ chat_history = []
17
+ def chatbot_response(user_input):
18
+ global chat_history
19
+ response = chatbot_model(f"User: {user_input} Chatbot:", max_length=100, num_return_sequences=1)
20
+ chat_history.append(f"User: {user_input}")
21
+ chat_history.append(f"Bot: {response[0]['generated_text']}")
22
+ return "\n".join(chat_history)
23
+
24
+ # Summarization Function
25
+ def summarize_text(text):
26
+ summary = summarization_model(text, max_length=150, min_length=50, do_sample=False)
27
+ return summary[0]["summary_text"]
28
+
29
+ # Text-to-Speech Function
30
+ def text_to_speech(text):
31
+ audio_output = text_to_speech_model(text)
32
+ return audio_output["audio"]
33
+
34
+ # Create Gradio Interface with Tabs
35
+ with gr.Blocks(theme="dark") as app:
36
+ gr.Markdown("# 🚀 Multi-Tab Language Application")
37
+
38
+ with gr.Tabs():
39
+ # Sentiment Analysis Tab
40
+ with gr.Tab("Sentiment Analysis"):
41
+ gr.Markdown("## 📊 Sentiment Analysis")
42
+ text_input = gr.Textbox(label="Enter text:")
43
+ sentiment_output = gr.Textbox(label="Sentiment")
44
+ confidence_output = gr.Textbox(label="Confidence Score")
45
+ analyze_button = gr.Button("Analyze")
46
+
47
+ analyze_button.click(analyze_sentiment, inputs=text_input, outputs=[sentiment_output, confidence_output])
48
+
49
+ # Chatbot Tab
50
+ with gr.Tab("Chatbot"):
51
+ gr.Markdown("## 🤖 Chatbot")
52
+ chatbot_input = gr.Textbox(label="Chat with AI")
53
+ chatbot_output = gr.Textbox(label="Response", interactive=False)
54
+ chat_button = gr.Button("Send")
55
+
56
+ chat_button.click(chatbot_response, inputs=chatbot_input, outputs=chatbot_output)
57
+
58
+ # Summarization Tab
59
+ with gr.Tab("Summarization"):
60
+ gr.Markdown("## ✍️ Text Summarization")
61
+ summary_input = gr.Textbox(label="Enter long text:", lines=5)
62
+ summary_output = gr.Textbox(label="Summary", interactive=False)
63
+ summarize_button = gr.Button("Summarize")
64
+
65
+ summarize_button.click(summarize_text, inputs=summary_input, outputs=summary_output)
66
+
67
+ # Text-to-Speech Tab
68
+ with gr.Tab("Text-to-Speech"):
69
+ gr.Markdown("## 🎙️ Text-to-Speech")
70
+ tts_input = gr.Textbox(label="Enter text to convert to speech:")
71
+ tts_output = gr.Audio(label="Generated Speech", autoplay=True)
72
+ tts_button = gr.Button("Convert to Speech")
73
+
74
+ tts_button.click(text_to_speech, inputs=tts_input, outputs=tts_output)
75
+
76
+ # Launch the application
77
+ app.launch()