Update app.py
Browse files
app.py
CHANGED
@@ -2,7 +2,6 @@ import os
|
|
2 |
import gradio as gr
|
3 |
from groq import Groq
|
4 |
|
5 |
-
|
6 |
# Retrieve API key from environment variable
|
7 |
api_key = os.getenv("GROQ_API_KEY")
|
8 |
client = Groq(api_key=api_key)
|
@@ -10,10 +9,6 @@ client = Groq(api_key=api_key)
|
|
10 |
if not api_key:
|
11 |
raise ValueError("API key not found. Please set the GROQ_API_KEY environment variable.")
|
12 |
|
13 |
-
#client = Groq(api_key=api_key)
|
14 |
-
|
15 |
-
chat_history = []
|
16 |
-
|
17 |
def transcribe_audio(file_path):
|
18 |
with open(file_path, "rb") as file:
|
19 |
transcription = client.audio.transcriptions.create(
|
@@ -44,8 +39,7 @@ def get_chat_completion(prompt):
|
|
44 |
response += chunk.choices[0].delta.content or ""
|
45 |
return response
|
46 |
|
47 |
-
def process_input(audio_file, text_input):
|
48 |
-
global chat_history
|
49 |
if audio_file is not None:
|
50 |
transcription_text = transcribe_audio(audio_file)
|
51 |
else:
|
@@ -59,23 +53,26 @@ def process_input(audio_file, text_input):
|
|
59 |
formatted_history = "\n".join([f"{role}: {content}\n" for role, content in chat_history])
|
60 |
|
61 |
# Return chat history and instructions to clear inputs
|
62 |
-
return formatted_history, gr.update(value=None), gr.update(value='')
|
63 |
|
64 |
# Create Gradio interface
|
65 |
interface = gr.Interface(
|
66 |
fn=process_input,
|
67 |
inputs=[
|
68 |
gr.Audio(type="filepath", label="Upload Audio or Record"),
|
69 |
-
gr.Textbox(lines=2, placeholder="Or type text here", label="Text Input")
|
|
|
70 |
],
|
71 |
outputs=[
|
72 |
gr.Textbox(label="Chat History", lines=20),
|
73 |
gr.Audio(visible=False), # Hidden output to reset audio input
|
74 |
-
gr.Textbox(visible=False) # Hidden output to reset text input
|
|
|
75 |
],
|
76 |
title="Chat with Llama 3.1-8B With Text or Voice (Whisper Large-v3)",
|
77 |
-
description="Upload an audio file or type text to get a chat response based on the transcription."
|
|
|
78 |
)
|
79 |
|
80 |
if __name__ == "__main__":
|
81 |
-
interface.launch()
|
|
|
2 |
import gradio as gr
|
3 |
from groq import Groq
|
4 |
|
|
|
5 |
# Retrieve API key from environment variable
|
6 |
api_key = os.getenv("GROQ_API_KEY")
|
7 |
client = Groq(api_key=api_key)
|
|
|
9 |
if not api_key:
|
10 |
raise ValueError("API key not found. Please set the GROQ_API_KEY environment variable.")
|
11 |
|
|
|
|
|
|
|
|
|
12 |
def transcribe_audio(file_path):
|
13 |
with open(file_path, "rb") as file:
|
14 |
transcription = client.audio.transcriptions.create(
|
|
|
39 |
response += chunk.choices[0].delta.content or ""
|
40 |
return response
|
41 |
|
42 |
+
def process_input(audio_file, text_input, chat_history):
|
|
|
43 |
if audio_file is not None:
|
44 |
transcription_text = transcribe_audio(audio_file)
|
45 |
else:
|
|
|
53 |
formatted_history = "\n".join([f"{role}: {content}\n" for role, content in chat_history])
|
54 |
|
55 |
# Return chat history and instructions to clear inputs
|
56 |
+
return formatted_history, gr.update(value=None), gr.update(value=''), chat_history
|
57 |
|
58 |
# Create Gradio interface
|
59 |
interface = gr.Interface(
|
60 |
fn=process_input,
|
61 |
inputs=[
|
62 |
gr.Audio(type="filepath", label="Upload Audio or Record"),
|
63 |
+
gr.Textbox(lines=2, placeholder="Or type text here", label="Text Input"),
|
64 |
+
gr.State([]) # This is the chat history specific to the session
|
65 |
],
|
66 |
outputs=[
|
67 |
gr.Textbox(label="Chat History", lines=20),
|
68 |
gr.Audio(visible=False), # Hidden output to reset audio input
|
69 |
+
gr.Textbox(visible=False), # Hidden output to reset text input
|
70 |
+
gr.State() # Returning the updated chat history
|
71 |
],
|
72 |
title="Chat with Llama 3.1-8B With Text or Voice (Whisper Large-v3)",
|
73 |
+
description="Upload an audio file or type text to get a chat response based on the transcription.",
|
74 |
+
allow_flagging='never' # Prevent flagging to isolate sessions
|
75 |
)
|
76 |
|
77 |
if __name__ == "__main__":
|
78 |
+
interface.launch()
|