JARVIS / app.py
HayZun's picture
Update app.py
f01fe14 verified
raw
history blame
4.36 kB
import gradio as gr
from googletrans import Translator
from transformers import pipeline
import tempfile
import edge_tts
import inflect
translator = Translator()
number_to_word = inflect.engine()
# Modèle de langue français
model = pipeline("text-generation", model="dbddv01/gpt2-french-small")
async def transcribe(audio):
lang = "fr"
# Supposons que vous avez déjà défini la fonction de transcription transcribe() dans votre code
text = transcribe(audio)
return text
async def respond(audio):
user = await transcribe(audio)
# Traduire l'entrée utilisateur en français
user_fr = translator.translate(user, src='en', dest='fr').text
reply = model(user_fr, max_length=50, num_return_sequences=1)[0]['generated_text']
# Traduire la réponse en anglais
reply_en = translator.translate(reply, src='fr', dest='en').text
communicate = edge_tts.Communicate(reply_en)
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
tmp_path = tmp_file.name
await communicate.save(tmp_path)
yield tmp_path
DESCRIPTION = """ # <center><b>JARVIS⚡</b></center>
### <center>A personal Assistant of Tony Stark for YOU
### <center>Voice Chat with your personal Assistant</center>
"""
MORE = """ ## TRY Other Models
### Instant Video: Create Amazing Videos in 5 Second -> https://huggingface.co/spaces/KingNish/Instant-Video
### Instant Image: 4k images in 5 Second -> https://huggingface.co/spaces/KingNish/Instant-Image
"""
FAST = """## Fastest Model"""
base_loaded = "mistralai/Mixtral-8x7B-Instruct-v0.1"
client1 = InferenceClient(base_loaded)
system_instructions1 = "<s>[SYSTEM] Répondez comme le vrai Jarvis JARVIS, fabriqué par 'Tony Stark'. La conversation doit être très courte, claire, amicale et concise. Le texte fourni est une demande de réponse spécifique de votre part, en tant qu'assistant virtuel. Il vous demande de fournir des réponses amicales comme si vous étiez le personnage Jarvis, créé par « Tony Stark ». On s'attend à ce que j'évite les présentations et que je réponde directement à la requête, en développant tous les aspects de la demande. En tant qu'assistant doté d'une IA, votre tâche consiste à générer des réponses qui semblent avoir été créées par l'utilisateur et non par un assistant doté d'une IA.[USER]"
async def generate1(prompt):
generate_kwargs = dict(
temperature=0.7,
max_new_tokens=512,
top_p=0.95,
repetition_penalty=1,
do_sample=False,
)
formatted_prompt = system_instructions1 + prompt + "[JARVIS]"
stream = client1.text_generation(
formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=True)
output = ""
for response in stream:
if not response.token.text == "</s>":
output += response.token.text
communicate = edge_tts.Communicate(output)
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
tmp_path = tmp_file.name
await communicate.save(tmp_path)
yield tmp_path
with gr.Blocks(css="style.css") as demo:
gr.Markdown(DESCRIPTION)
with gr.Row():
user_input = gr.Audio(label="Voice Chat (BETA)", type="filepath")
output_audio = gr.Audio(label="JARVIS", type="filepath",
interactive=False,
autoplay=True,
elem_classes="audio")
with gr.Row():
translate_btn = gr.Button("Response")
translate_btn.click(fn=respond, inputs=user_input,
outputs=output_audio, api_name=False)
gr.Markdown(FAST)
with gr.Row():
user_input = gr.Textbox(label="Prompt", value="What is Wikipedia")
input_text = gr.Textbox(label="Input Text", elem_id="important")
output_audio = gr.Audio(label="JARVIS", type="filepath",
interactive=False,
autoplay=True,
elem_classes="audio")
with gr.Row():
translate_btn = gr.Button("Response")
translate_btn.click(fn=generate1, inputs=user_input,
outputs=output_audio, api_name="translate")
gr.Markdown(MORE)
if __name__ == "__main__":
demo.queue(max_size=200).launch()