Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 4 |
+
import whisper # Pour le Speech-to-Text
|
| 5 |
+
from gtts import gTTS # Pour le Text-to-Speech
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
# --- 1. Chargement des modèles ---
|
| 9 |
+
# Ton modèle Gheya
|
| 10 |
+
model_name = "Finisha-F-scratch/Gheya-dialogue-v1"
|
| 11 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 12 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
| 13 |
+
|
| 14 |
+
# Modèle de transcription (Whisper base est rapide pour le temps réel)
|
| 15 |
+
stt_model = whisper.load_model("base")
|
| 16 |
+
|
| 17 |
+
def voice_chat(audio_path):
|
| 18 |
+
if audio_path is None:
|
| 19 |
+
return None, "Veuillez enregistrer un message."
|
| 20 |
+
|
| 21 |
+
# ÉTAPE A : Transcription (Audio -> Texte)
|
| 22 |
+
result = stt_model.transcribe(audio_path)
|
| 23 |
+
user_text = result["text"]
|
| 24 |
+
|
| 25 |
+
# ÉTAPE B : Génération Gheya (Texte -> Texte)
|
| 26 |
+
input_ids = tokenizer(user_text, return_tensors="pt").input_ids
|
| 27 |
+
outputs = model.generate(
|
| 28 |
+
input_ids,
|
| 29 |
+
max_new_tokens=150,
|
| 30 |
+
do_sample=True,
|
| 31 |
+
temperature=0.7
|
| 32 |
+
)
|
| 33 |
+
response_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 34 |
+
|
| 35 |
+
# ÉTAPE C : Synthèse Vocale (Texte -> Audio)
|
| 36 |
+
tts = gTTS(text=response_text, lang='fr')
|
| 37 |
+
output_audio_path = "response.mp3"
|
| 38 |
+
tts.save(output_audio_path)
|
| 39 |
+
|
| 40 |
+
return output_audio_path, response_text
|
| 41 |
+
|
| 42 |
+
# --- 2. Interface Gradio ---
|
| 43 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 44 |
+
gr.Markdown("# 🎙️ Gheya Voice Call")
|
| 45 |
+
gr.Markdown("Parlez directement avec le modèle Gheya-dialogue-v1.")
|
| 46 |
+
|
| 47 |
+
with gr.Row():
|
| 48 |
+
with gr.Column():
|
| 49 |
+
# Entrée audio (Microphone)
|
| 50 |
+
input_audio = gr.Audio(sources="microphone", type="filepath", label="Appuyez pour parler")
|
| 51 |
+
submit_btn = gr.Button("Envoyer l'appel", variant="primary")
|
| 52 |
+
|
| 53 |
+
with gr.Column():
|
| 54 |
+
# Sortie audio et transcription textuelle pour suivi
|
| 55 |
+
output_audio = gr.Audio(label="Gheya vous répond", autoplay=True)
|
| 56 |
+
output_text = gr.Textbox(label="Transcription de la réponse")
|
| 57 |
+
|
| 58 |
+
# Logique de clic
|
| 59 |
+
submit_btn.click(
|
| 60 |
+
fn=voice_chat,
|
| 61 |
+
inputs=input_audio,
|
| 62 |
+
outputs=[output_audio, output_text]
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
if __name__ == "__main__":
|
| 66 |
+
demo.launch()
|
| 67 |
+
|