nikajoon commited on
Commit
0e60811
1 Parent(s): 1b8f0eb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -62
app.py CHANGED
@@ -1,74 +1,65 @@
 
 
1
  import gradio as gr
2
- import wave
3
- import numpy as np
4
- from io import BytesIO
5
- from huggingface_hub import hf_hub_download
6
- from piper import PiperVoice
7
- from transformers import pipeline
8
- import hazm
9
- import typing
10
 
11
- normalizer = hazm.Normalizer()
12
- sent_tokenizer = hazm.SentenceTokenizer()
13
- word_tokenizer = hazm.WordTokenizer()
14
 
15
- tagger_path = hf_hub_download(repo_id="gyroing/HAZM_POS_TAGGER", filename="pos_tagger.model")
16
- tagger = hazm.POSTagger(model=tagger_path)
17
- model_path = hf_hub_download(repo_id="gyroing/Persian-Piper-Model-gyro", filename="fa_IR-gyro-medium.onnx")
18
- config_path = hf_hub_download(repo_id="gyroing/Persian-Piper-Model-gyro", filename="fa_IR-gyro-medium.onnx.json")
19
- voice = PiperVoice.load(model_path, config_path)
20
 
21
- def preprocess_text(text: str) -> typing.List[typing.List[str]]:
22
- """Split/normalize text into sentences/words with hazm"""
23
- text = normalizer.normalize(text)
24
- processed_sentences = []
25
 
26
- for sentence in sent_tokenizer.tokenize(text):
27
- words = word_tokenizer.tokenize(sentence)
28
- processed_words = fix_words(words)
29
- processed_sentences.append(" ".join(processed_words))
30
- return " ".join(processed_sentences)
31
- def fix_words(words: typing.List[str]) -> typing.List[str]:
32
- fixed_words = []
33
 
34
- for word, pos in tagger.tag(words):
35
- if pos[-1] == "Z":
36
- if word[-1] != "ِ":
37
- if (word[-1] == "ه") and (word[-2] != "ا"):
38
- word += "‌ی"
39
- word += "ِ"
40
-
41
 
42
- fixed_words.append(word)
 
43
 
44
- return fixed_words
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
 
46
- def synthesize_speech(text):
 
47
 
 
 
 
48
 
49
- # Create an in-memory buffer for the WAV file
50
- buffer = BytesIO()
51
- with wave.open(buffer, 'wb') as wav_file:
52
- wav_file.setframerate(voice.config.sample_rate)
53
- wav_file.setsampwidth(2) # 16-bit
54
- wav_file.setnchannels(1) # mono
55
 
56
- # Synthesize speech
57
- eztext = preprocess_text(text)
58
- voice.synthesize(eztext, wav_file)
59
-
60
- # Convert buffer to NumPy array for Gradio output
61
- buffer.seek(0)
62
- audio_data = np.frombuffer(buffer.read(), dtype=np.int16)
63
-
64
- return audio_data.tobytes()
65
-
66
- # Using Gradio Blocks
67
- with gr.Blocks(theme=gr.themes.Base()) as blocks:
68
- input_text = gr.Textbox(label="Input")
69
- output_audio = gr.Audio(label="Output", type="numpy")
70
- submit_button = gr.Button("Synthesize")
71
-
72
- submit_button.click(synthesize_speech, inputs=input_text, outputs=[output_audio])
73
- # Run the app
74
- blocks.launch()
 
1
+ from fastapi import FastAPI, HTTPException
2
+ from pydantic import BaseModel
3
  import gradio as gr
4
+ import uvicorn
5
+ import requests
6
+ import os
7
+ from dotenv import load_dotenv
8
+ import threading
 
 
 
9
 
10
+ # بارگذاری متغیرهای محیطی از فایل .env
11
+ load_dotenv()
 
12
 
13
+ # ایجاد یک اپلیکیشن FastAPI
14
+ app = FastAPI()
 
 
 
15
 
16
+ # تعریف Gradio Interface
17
+ def tts_model(text):
18
+ # اینجا می‌توانید کد اصلی تبدیل متن به گفتار را قرار دهید.
19
+ return f"صوت برای متن: {text}"
20
 
21
+ # پیکربندی رابط کاربری Gradio
22
+ iface = gr.Interface(
23
+ fn=tts_model,
24
+ inputs="text",
25
+ outputs="audio",
26
+ title="Text to Speech"
27
+ )
28
 
29
+ # مدل داده‌ای برای ورودی متنی در FastAPI
30
+ class TextInput(BaseModel):
31
+ text: str
 
 
 
 
32
 
33
+ # خواندن URL از فایل .env
34
+ hugging_face_api_url = os.getenv("HUGGING_FACE_SPACE_API_URL")
35
 
36
+ # روت FastAPI برای تبدیل متن به گفتار
37
+ @app.post("/api/tts/")
38
+ async def convert_text_to_speech(input_text: TextInput):
39
+ try:
40
+ # ارسال درخواست به Hugging Face Space API
41
+ response = requests.post(
42
+ hugging_face_api_url,
43
+ json={"text": input_text.text}
44
+ )
45
+
46
+ if response.status_code == 200:
47
+ # دریافت و بازگرداندن نتیجه
48
+ return {"audio": response.content} # یا هر پردازشی که نیاز دارید
49
+ else:
50
+ raise HTTPException(status_code=response.status_code, detail="Error in TTS API")
51
 
52
+ except Exception as e:
53
+ raise HTTPException(status_code=500, detail=str(e))
54
 
55
+ # راه‌اندازی Gradio در یک ترد جداگانه
56
+ def start_gradio():
57
+ iface.launch(server_name="0.0.0.0", server_port=7860, share=True)
58
 
59
+ if __name__ == "__main__":
60
+ # اجرای Gradio در یک ترد جداگانه
61
+ gradio_thread = threading.Thread(target=start_gradio)
62
+ gradio_thread.start()
 
 
63
 
64
+ # اجرای FastAPI
65
+ uvicorn.run(app, host="0.0.0.0", port=8000)