hebaadel commited on
Commit
e5d3d8f
·
verified ·
1 Parent(s): 1b89875

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -0
app.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from datasets import load_dataset
2
+ from transformers import pipeline
3
+ import soundfile as sf
4
+ import torch
5
+ import gradio as gr
6
+ import numpy as np
7
+
8
+ def predict_image(image):
9
+ pipe = pipeline("image-classification", model="google/vit-base-patch16-224")
10
+ ClassifedImage=pipe(image)
11
+
12
+ result=ClassifedImage[0]['label']
13
+ return result
14
+
15
+ def translate_to_arabic(text):
16
+ pipe = pipeline("translation", model="Helsinki-NLP/opus-mt-en-ar")
17
+ result=pipe(text , max_length=100)
18
+ return result[0]['translation_text']
19
+
20
+ def text_to_speech(text):
21
+ pipe = pipeline("text-to-speech", model="MBZUAI/speecht5_tts_clartts_ar")
22
+ embedding_dataset=load_dataset("herwoww/arabic_xvector_embeddings" , split="validation")
23
+ speaker_embedding=torch.tensor(embedding_dataset[100]['speaker_embeddings']).unsqueeze(0)
24
+ speech=pipe(text , forward_params={'speaker_embeddings':speaker_embedding})
25
+
26
+ return (speech['sampling_rate'],np.array(speech['audio'], dtype=np.float32))
27
+
28
+ from PIL import Image
29
+ with gr.Blocks() as app:
30
+ gr.Markdown("Image Classification, Arabic Translation, TTS")
31
+
32
+ with gr.Row():
33
+ with gr.Column():
34
+ image_input=gr.Image(type="pil",label="Upload the Image to classify it" )
35
+ classify_image=gr.Button("Classify the Image")
36
+ pred=gr.Textbox(label="Classifcation Result")
37
+
38
+ classify_image.click(fn=predict_image , inputs=image_input , outputs=pred)
39
+
40
+ with gr.Row():
41
+ translated_output=gr.Textbox(label="Translated Text")
42
+ translate_btn=gr.Button("Translate to Arabic")
43
+
44
+ translate_btn.click(fn=translate_to_arabic , inputs=pred , outputs=translated_output)
45
+
46
+ with gr.Row():
47
+ tts_btn=gr.Button("Convert to Speech")
48
+ audio_output=gr.Audio(label="Audio Output")
49
+
50
+ tts_btn.click(fn=text_to_speech , inputs=translated_output , outputs=audio_output)
51
+
52
+ app.launch()