Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,29 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
)
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|