candenizkocak commited on
Commit
1f2ea7d
·
verified ·
1 Parent(s): 3911815

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -0
app.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ 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)
9
+
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(
20
+ file=(file_path, file.read()),
21
+ model="whisper-large-v3",
22
+ response_format="verbose_json",
23
+ )
24
+ return transcription.text
25
+
26
+ def get_chat_completion(prompt):
27
+ completion = client.chat.completions.create(
28
+ model="llama-3.1-8b-instant",
29
+ messages=[
30
+ {
31
+ "role": "user",
32
+ "content": prompt
33
+ }
34
+ ],
35
+ temperature=1,
36
+ max_tokens=1024,
37
+ top_p=1,
38
+ stream=True,
39
+ stop=None,
40
+ )
41
+
42
+ response = ""
43
+ for chunk in completion:
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:
52
+ transcription_text = text_input
53
+
54
+ chat_response = get_chat_completion(transcription_text)
55
+ chat_history.append(("👤", transcription_text))
56
+ chat_history.append(("🤖", chat_response))
57
+
58
+ # Format chat history for display
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()