Update app.py
Browse files
app.py
CHANGED
@@ -1,74 +1,65 @@
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
-
import
|
3 |
-
import
|
4 |
-
|
5 |
-
from
|
6 |
-
|
7 |
-
from transformers import pipeline
|
8 |
-
import hazm
|
9 |
-
import typing
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
word_tokenizer = hazm.WordTokenizer()
|
14 |
|
15 |
-
|
16 |
-
|
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 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
if (word[-1] == "ه") and (word[-2] != "ا"):
|
38 |
-
word += "ی"
|
39 |
-
word += "ِ"
|
40 |
-
|
41 |
|
42 |
-
|
|
|
43 |
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
-
|
|
|
47 |
|
|
|
|
|
|
|
48 |
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
wav_file.setsampwidth(2) # 16-bit
|
54 |
-
wav_file.setnchannels(1) # mono
|
55 |
|
56 |
-
|
57 |
-
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|