SumbalFatima1122 commited on
Commit
2d17967
·
verified ·
1 Parent(s): 2d18678

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -0
app.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Importing necessary libraries
2
+ import os
3
+ import whisper
4
+ from groq import Groq
5
+ from gtts import gTTS
6
+ import gradio as gr
7
+
8
+ # Step 1: Load the Whisper model
9
+ print("Loading Whisper model...")
10
+ whisper_model = whisper.load_model("base")
11
+
12
+ # Step 2: Initialize the Groq API client
13
+ GROP_API_KEY = "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"
14
+ client = Groq(api_key=GROP_API_KEY)
15
+
16
+ # Function to transcribe audio using Whisper
17
+ def transcribe_audio(audio_file):
18
+ print("Transcribing audio...")
19
+ result = whisper_model.transcribe(audio_file)
20
+ return result['text']
21
+
22
+ # Function to interact with Groq's LLM
23
+ def get_llm_response(user_text):
24
+ print(f"Sending request to Groq API with input: {user_text}")
25
+ try:
26
+ chat_completion = client.chat.completions.create(
27
+ messages=[{"role": "user", "content": user_text}],
28
+ model="llama3-8b-8192",
29
+ )
30
+ print(f"Groq API full response: {chat_completion}")
31
+ return chat_completion.choices[0].message.content
32
+ except Exception as e:
33
+ print(f"Error while fetching response from Groq API: {e}")
34
+ return f"Error occurred: {e}"
35
+
36
+ # Function to convert text to speech using gTTS
37
+ def text_to_speech(response_text, output_path="response.mp3"):
38
+ print("Converting text to speech...")
39
+ tts = gTTS(response_text)
40
+ tts.save(output_path)
41
+ return output_path
42
+
43
+ # Complete chat pipeline function
44
+ def chat_pipeline(audio_input):
45
+ # Step 1: Transcribe audio
46
+ transcribed_text = transcribe_audio(audio_input)
47
+
48
+ # Step 2: Get response from LLM
49
+ llm_response = get_llm_response(transcribed_text)
50
+
51
+ # Step 3: Convert LLM response to audio
52
+ response_audio_path = text_to_speech(llm_response)
53
+
54
+ return transcribed_text, llm_response, response_audio_path
55
+
56
+ # Gradio Interface
57
+ print("Setting up Gradio interface...")
58
+ interface = gr.Interface(
59
+ fn=chat_pipeline,
60
+ inputs=gr.Audio(type="filepath", label="Record or Upload Audio"),
61
+ outputs=[
62
+ gr.Textbox(label="Transcribed Text"),
63
+ gr.Textbox(label="LLM Response"),
64
+ gr.Audio(label="Response Audio"),
65
+ ],
66
+ live=True
67
+ )
68
+
69
+ # Launch the Gradio app
70
+ print("Launching Gradio app...")
71
+ interface.launch()
72
+