Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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 |
-
|
|
|
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
sentiment =pipeline("text-classification", model="tabularisai/multilingual-sentiment-analysis")
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
def get_summary(text):
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
| 30 |
|
| 31 |
def text_to_speech(text):
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
|
|
|
| 36 |
|
| 37 |
-
with gr.Blocks() as
|
|
|
|
|
|
|
| 38 |
with gr.Tab("Sentiment Analysis"):
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
with gr.Tab("Summarization"):
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
|
| 52 |
with gr.Tab("Text to Speech"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 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()
|
|
|
|
|
|
|
|
|
|
|
|