iMeshal commited on
Commit
77a33bf
·
verified ·
1 Parent(s): c0069dd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -41
app.py CHANGED
@@ -1,59 +1,95 @@
1
- import warnings
2
- warnings.filterwarnings('ignore')
3
- import transformers
4
  import gradio as gr
5
  from transformers import pipeline
6
- from TTS.api import TTS
 
7
 
8
- #1 - Sentiment Analysis Tab:
9
- sentiment =pipeline("text-classification", model="tabularisai/multilingual-sentiment-analysis")
10
 
11
- def get_sentiment(text):
12
- result = sentiment(text)
13
- label = result[0]['label']
14
- score = result[0]['score']
15
- return label, score
 
 
 
 
 
 
 
 
 
 
16
 
17
- #2 - Summarization Tab:
18
- summary_pipe = pipeline("summarization", model="google/pegasus-cnn_dailymail")
 
 
 
 
 
 
 
 
19
 
20
  def get_summary(text):
21
- max_len = int(len(text.split()) * 0.35)
22
- max_len = max(5, min(max_len, 512))
23
- min_len = int(len(text.split()) * 0.2)
24
-
25
- result = summary_pipe(text, max_length=max_len, min_length=min_len, do_sample=False)
26
- return result[0]['summary_text']
 
 
 
 
27
 
28
- #3 - text-to-Speech Tab:
29
- tts_model= TTS("tts_models/en/ljspeech/tacotron2-DDC", gpu=False)
 
 
30
 
31
  def text_to_speech(text):
32
- filename = "output.wav"
33
- tts_model.tts_to_file(text=text, file_path=filename)
34
- return filename
 
 
 
 
 
 
 
 
35
 
 
36
 
37
- with gr.Blocks() as AI_applications:
 
 
38
  with gr.Tab("Sentiment Analysis"):
39
- sentiment_input = gr.Textbox(label="Enter Text")
40
- output_label = gr.Text(label="Sentiment Label")
41
- output_score = gr.Text(label="Confidence Score")
42
- analyze_button = gr.Button("Analyze Sentiment")
43
- analyze_button.click(fn=get_sentiment,inputs=sentiment_input ,outputs=[output_label, output_score])
 
 
 
44
 
45
  with gr.Tab("Summarization"):
46
-
47
- summary_input = gr.Textbox(label="Enter Long Text", lines=10, placeholder="Paste your text here...")
48
- output_summary = gr.Textbox(label="Summary", lines=5)
49
- summarize_btn = gr.Button("Summarize")
50
- summarize_btn .click(fn=get_summary, inputs=summary_input, outputs=output_summary)
51
 
52
  with gr.Tab("Text to Speech"):
 
 
 
 
 
53
 
54
- tts_input = gr.Textbox(label="Enter Text to Speak", lines=4, placeholder="Type something...")
55
- output_audio = gr.Audio(label="Generated Speech", type="filepath")
56
- generate_btn = gr.Button("Generate Speech")
57
- generate_btn.click(fn=text_to_speech, inputs=tts_input , outputs=output_audio)
58
-
59
- AI_applications.launch()
 
 
 
 
1
  import gradio as gr
2
  from transformers import pipeline
3
+ import scipy.io.wavfile
4
+ import numpy as np
5
 
6
+ # --- تحميل النماذج (تم اختيار نماذج خفيفة وسريعة للنسخة المجانية) ---
 
7
 
8
+ # 1. Sentiment Analysis:
9
+ # استبدلنا النموذج بنموذج DistilBERT خفيف وسريع ويدعم لغات متعددة
10
+ sentiment_pipeline = pipeline("text-classification", model="lxyuan/distilbert-base-multilingual-cased-sentiments-student")
11
+
12
+ # 2. Summarization:
13
+ # استبدلنا Pegasus (الثقيل جداً) بنموذج DistilBART (سريع وممتاز للنسخة المجانية)
14
+ summarizer_pipeline = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")
15
+
16
+ # 3. Text-to-Speech:
17
+ # استبدلنا مكتبة TTS الخارجية بنموذج مدمج داخل transformers لتوفير الذاكرة وتجنب الأخطاء
18
+ # نستخدم نموذج MMS من فيسبوك للغة الإنجليزية (سريع جداً)
19
+ tts_pipeline = pipeline("text-to-speech", model="facebook/mms-tts-eng")
20
+
21
+
22
+ # --- الدوال (Functions) ---
23
 
24
+ def get_sentiment(text):
25
+ if not text:
26
+ return "Please enter text", "0.0"
27
+ try:
28
+ result = sentiment_pipeline(text)
29
+ label = result[0]['label']
30
+ score = round(result[0]['score'], 4)
31
+ return label, str(score)
32
+ except Exception as e:
33
+ return "Error", str(e)
34
 
35
  def get_summary(text):
36
+ if not text:
37
+ return "Please enter text to summarize."
38
+ try:
39
+ # حساب أطوال ذكية للملخص بناءً على النص المدخل
40
+ input_len = len(text.split())
41
+ max_len = max(10, int(input_len * 0.5))
42
+ min_len = max(5, int(input_len * 0.1))
43
+
44
+ # التأكد من الحدود القصوى للنموذج
45
+ max_len = min(max_len, 100)
46
 
47
+ result = summarizer_pipeline(text, max_length=max_len, min_length=min_len, do_sample=False)
48
+ return result[0]['summary_text']
49
+ except Exception as e:
50
+ return f"Error in summarization: {str(e)}"
51
 
52
  def text_to_speech(text):
53
+ if not text:
54
+ return None
55
+ try:
56
+ speech = tts_pipeline(text)
57
+ # تحويل البيانات الصوتية لنسق يفهمه Gradio
58
+ audio_data = speech["audio"]
59
+ sampling_rate = speech["sampling_rate"]
60
+ return (sampling_rate, audio_data.T) # Transpose for Gradio compatibility
61
+ except Exception as e:
62
+ print(f"TTS Error: {e}")
63
+ return None
64
 
65
+ # --- واجهة Gradio ---
66
 
67
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
68
+ gr.Markdown("# 🚀 AI Multi-Tool (Lightweight Version)")
69
+
70
  with gr.Tab("Sentiment Analysis"):
71
+ gr.Markdown("Analyze the emotion of the text (Positive/Negative).")
72
+ with gr.Row():
73
+ sent_input = gr.Textbox(label="Enter Text", placeholder="I love using AI models!", lines=2)
74
+ sent_btn = gr.Button("Analyze", variant="primary")
75
+ with gr.Row():
76
+ sent_label = gr.Textbox(label="Label")
77
+ sent_score = gr.Textbox(label="Confidence Score")
78
+ sent_btn.click(fn=get_sentiment, inputs=sent_input, outputs=[sent_label, sent_score])
79
 
80
  with gr.Tab("Summarization"):
81
+ gr.Markdown("Summarize long articles quickly.")
82
+ sum_input = gr.Textbox(label="Long Text", lines=8, placeholder="Paste your article here...")
83
+ sum_btn = gr.Button("Summarize", variant="primary")
84
+ sum_output = gr.Textbox(label="Summary", lines=4)
85
+ sum_btn.click(fn=get_summary, inputs=sum_input, outputs=sum_output)
86
 
87
  with gr.Tab("Text to Speech"):
88
+ gr.Markdown("Convert text to audio (English).")
89
+ tts_input = gr.Textbox(label="Text to Speak", lines=3, placeholder="Hello, how are you doing today?")
90
+ tts_btn = gr.Button("Generate Audio", variant="primary")
91
+ tts_output = gr.Audio(label="Output Audio")
92
+ tts_btn.click(fn=text_to_speech, inputs=tts_input, outputs=tts_output)
93
 
94
+ if __name__ == "__main__":
95
+ demo.launch()