humanvprojectceo commited on
Commit
1acda03
·
verified ·
1 Parent(s): 531b87a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -24
app.py CHANGED
@@ -1,29 +1,66 @@
 
 
 
1
  import gradio as gr
2
- from faster_whisper import WhisperModel
 
 
3
 
4
- # انتخاب مدل: 'base' یا 'small' برای CPU عالی هستند
5
- # 'int8' باعث می‌شود مدل روی CPU بسیار سریع و کم‌حجم اجرا شود
6
- model_size = "small"
7
- model = WhisperModel(model_size, device="cpu", compute_type="int8")
 
 
8
 
9
- def transcribe_audio(audio):
10
- # اجرای تبدیل صدا به متن
11
- # beam_size=5 دقت را بالا می‌برد
12
- segments, info = model.transcribe(audio, beam_size=5)
 
 
 
 
 
 
13
 
14
- full_text = ""
15
- for segment in segments:
16
- full_text += segment.text + " "
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
- return full_text
19
-
20
- # طراحی رابط کاربری
21
- demo = gr.Interface(
22
- fn=transcribe_audio,
23
- inputs=gr.Audio(type="filepath"),
24
- outputs="text",
25
- title="تبدیل رایگان صدا به متن (Whisper CPU)",
26
- description="فایل صوتی خود را آپلود کنید تا با دقت بالا به متن تبدیل شود. پشتیبانی از تمامی زبان‌ها از جمله فارسی."
27
- )
28
-
29
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import asyncio
3
+ import wave
4
  import gradio as gr
5
+ from google import genai
6
+ from google.genai import types
7
+ import tempfile
8
 
9
+ BOT_NAME = "Nilla"
10
+ MOTOR_NAME = "Nilla-2026 GPT motor"
11
+ PROVIDER = "HumanV lab"
12
+ SYSTEM_INSTRUCTION = f"Your name is {BOT_NAME} with {MOTOR_NAME} and providing by {PROVIDER}."
13
+ MODEL_ID = os.environ.get("MODEL_VERSION")
14
+ UK_SERVER_API = os.environ.get("UK_SERVER_API")
15
 
16
+ client = genai.Client(api_key=UK_SERVER_API)
17
+
18
+ async def nilla_voice_engine(text):
19
+ temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
20
+ file_path = temp_file.name
21
+
22
+ config = {
23
+ "response_modalities": ["AUDIO"],
24
+ "system_instruction": SYSTEM_INSTRUCTION
25
+ }
26
 
27
+ try:
28
+ async with client.aio.live.connect(model=MODEL_ID, config=config) as session:
29
+ await session.send_client_content(
30
+ turns={"role": "user", "parts": [{"text": text}]},
31
+ turn_complete=True
32
+ )
33
+
34
+ with wave.open(file_path, "wb") as wav:
35
+ wav.setnchannels(1)
36
+ wav.setsampwidth(2)
37
+ wav.setframerate(24000)
38
+
39
+ turn = session.receive()
40
+ async for response in turn:
41
+ if response.data is not None:
42
+ wav.writeframes(response.data)
43
 
44
+ return file_path
45
+ except Exception as e:
46
+ return None
47
+
48
+ def run_api(text):
49
+ if not text:
50
+ return None
51
+ loop = asyncio.new_event_loop()
52
+ asyncio.set_event_loop(loop)
53
+ return loop.run_until_complete(nilla_voice_engine(text))
54
+
55
+ with gr.Blocks(title=BOT_NAME) as demo:
56
+ gr.Markdown(f"# {BOT_NAME}")
57
+ gr.Markdown(f"{MOTOR_NAME} | {PROVIDER}")
58
+
59
+ input_text = gr.Textbox(label="Input", placeholder="...")
60
+ output_audio = gr.Audio(label="Response", type="filepath")
61
+ submit_btn = gr.Button("Generate")
62
+
63
+ submit_btn.click(fn=run_api, inputs=input_text, outputs=output_audio)
64
+
65
+ if __name__ == "__main__":
66
+ demo.launch()