caarleexx commited on
Commit
5d0198d
·
verified ·
1 Parent(s): 4cc493f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -13
app.py CHANGED
@@ -1,16 +1,79 @@
 
1
  import gradio as gr
2
- import groq_gradio
3
  import os
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
- # Initialize Groq client
6
- client = Groq(
7
- api_key=os.environ.get("GROQ_API_KEY")
8
- )
9
-
10
- gr.load(
11
- name='llama-3.3-70b-versatile', # The specific model powered by Groq to use
12
- src=groq_gradio.registry, # Tells Gradio to use our custom interface registry as the source
13
- title='Groq-Gradio Integration', # The title shown at the top of our UI
14
- description="Chat with the Llama 3.3 70B model powered by Groq.", # Subtitle
15
- examples=["Explain quantum gravity to a 5-year old.", "How many R are there in the word Strawberry?"] # Pre-written prompts users can click to try
16
- ).launch() # Creates and starts the web server!
 
1
+
2
  import gradio as gr
3
+ from openai import OpenAI
4
  import os
5
+ from groq import Groq
6
+
7
+ # Configure suas chaves de API. É altamente recomendável usar os segredos do Hugging Face.
8
+ # Se estiver executando localmente, certifique-se de que suas variáveis de ambiente estejam definidas.
9
+ try:
10
+ groq_api_key = os.environ['GROQ_API_KEY']
11
+ except KeyError:
12
+ # Se a chave de API não estiver definida como um segredo, você pode inseri-la aqui.
13
+ # No entanto, isso não é recomendado por razões de segurança.
14
+ groq_api_key = "SUA_CHAVE_DE_API_DA_GROQ_AQUI"
15
+
16
+
17
+ client = Groq(api_key=groq_api_key)
18
+ openai_client = OpenAI() # A biblioteca openai é usada para a transcrição do Whisper
19
+
20
+ def transcribe_audio(audio_path):
21
+ """
22
+ Transcreve o áudio usando a API do Whisper da OpenAI.
23
+ """
24
+ if audio_path is None:
25
+ return ""
26
+ with open(audio_path, "rb") as audio_file:
27
+ transcription = openai_client.audio.transcriptions.create(
28
+ model="whisper-1",
29
+ file=audio_file
30
+ )
31
+ return transcription.text
32
+
33
+ def get_llm_response(user_input):
34
+ """
35
+ Obtém uma resposta de um modelo de linguagem grande usando a API da Groq.
36
+ """
37
+ if not user_input:
38
+ return "Por favor, faça uma pergunta."
39
+
40
+ chat_completion = client.chat.completions.create(
41
+ messages=[
42
+ {
43
+ "role": "system",
44
+ "content": "Você é um assistente prestativo."
45
+ },
46
+ {
47
+ "role": "user",
48
+ "content": user_input,
49
+ }
50
+ ],
51
+ model="llama3-8b-8192",
52
+ )
53
+ return chat_completion.choices[0].message.content
54
+
55
+ # Define a interface do Gradio
56
+ with gr.Blocks() as demo:
57
+ gr.Markdown("# Detecção Automática de Voz com Gradio e Groq")
58
+ gr.Markdown("Fale no microfone e o aplicativo irá transcrever e responder automaticamente.")
59
+
60
+ with gr.Row():
61
+ audio_input = gr.Audio(sources=["microphone"], type="filepath", label="Entrada de Áudio")
62
+ transcribed_text = gr.Textbox(label="Texto Transcrito")
63
+
64
+ with gr.Row():
65
+ llm_response = gr.Textbox(label="Resposta do LLM")
66
+
67
+ # Define as ações da interface
68
+ audio_input.stop_recording(
69
+ fn=transcribe_audio,
70
+ inputs=audio_input,
71
+ outputs=transcribed_text
72
+ ).then(
73
+ fn=get_llm_response,
74
+ inputs=transcribed_text,
75
+ outputs=llm_response
76
+ )
77
 
78
+ # Lança a interface do Gradio
79
+ demo.launch()