| import gradio as gr |
| from huggingface_hub import InferenceClient |
| import edge_tts |
| import tempfile |
| import asyncio |
|
|
| |
| client = InferenceClient("google/gemma-1.1-2b-it") |
|
|
| |
| async def text_to_speech(text, voice="fr-FR-DeniseNeural", rate=0, pitch=0): |
| if not text.strip(): |
| return None, "Veuillez entrer du texte." |
|
|
| rate_str = f"{rate:+d}%" |
| pitch_str = f"{pitch:+d}Hz" |
| communicate = edge_tts.Communicate(text, voice, rate=rate_str, pitch=pitch_str) |
|
|
| |
| with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file: |
| tmp_path = tmp_file.name |
| await communicate.save(tmp_path) |
| |
| return tmp_path |
|
|
| |
| def models(query): |
| messages = [{"role": "user", "content": f"[SYSTEM] You are a fast assistant. [USER] {query}"}] |
| response = "" |
|
|
| for message in client.chat_completion(messages, max_tokens=2048, stream=True): |
| token = message.choices[0].delta.content |
| response += token |
|
|
| |
| tts_path = asyncio.run(text_to_speech(response)) |
| return response, tts_path |
|
|
| |
| def nemo(query): |
| budget = 3 |
| message = f"""[INST] [SYSTEM] You are a deep-thinking assistant. |
| <count> {budget} </count> <step> Analyzing question... </step> <count> {budget-1} </count> |
| <answer> Here is your answer: {query} </answer> [/INST]""" |
|
|
| stream = client.text_generation(message, max_new_tokens=4096, stream=True) |
| output = "".join([response.token.text for response in stream]) |
|
|
| |
| tts_path = asyncio.run(text_to_speech(output)) |
| return output, tts_path |
|
|
| |
| description = "# Light ChatBox\n### Enter a question and get a response with voice!" |
|
|
| with gr.Blocks() as demo1: |
| gr.Interface(fn=models, inputs="text", outputs=["text", "audio"], description=description) |
|
|
| with gr.Blocks() as demo2: |
| gr.Interface(fn=nemo, inputs="text", outputs=["text", "audio"], description="Critical Thinker") |
|
|
| with gr.Blocks() as demo: |
| gr.TabbedInterface([demo1, demo2], ["Fast", "Critical"]) |
|
|
| demo.queue() |
| demo.launch() |