mishiawan commited on
Commit
25a9cec
·
verified ·
1 Parent(s): cf5acf5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -48
app.py CHANGED
@@ -1,54 +1,49 @@
1
  import os
2
- import whisper
3
- from gtts import gTTS
4
  from groq import Groq
5
- import gradio as gr
6
- from tempfile import NamedTemporaryFile
7
 
8
- # Set up Groq client
9
- client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
10
 
11
- # Load OpenAI Whisper model
12
- whisper_model = whisper.load_model("base")
13
-
14
- def voice_to_voice_chat(audio_file):
15
- # Step 1: Transcribe Audio Input
16
- transcription = whisper_model.transcribe(audio_file)["text"]
17
-
18
- # Step 2: Query Groq's LLM
19
- chat_completion = client.chat.completions.create(
20
- messages=[
21
- {"role": "user", "content": transcription}
22
- ],
23
- model="llama3-8b-8192",
24
- stream=False,
25
- )
26
- llm_response = chat_completion.choices[0].message.content
27
-
28
- # Step 3: Convert LLM Response to Audio
29
- tts = gTTS(llm_response)
30
- audio_output = NamedTemporaryFile(delete=False, suffix=".mp3")
31
- tts.save(audio_output.name)
32
-
33
- return llm_response, audio_output.name
34
-
35
- # Gradio Interface
36
- def chatbot_interface(audio_input):
37
- response, audio_response_file = voice_to_voice_chat(audio_input)
38
- return response, audio_response_file
39
-
40
- # Build Gradio App
41
- interface = gr.Interface(
42
- fn=chatbot_interface,
43
- inputs=gr.Audio(source="microphone", type="filepath", label="Speak into the Microphone"),
44
- outputs=[
45
- gr.Textbox(label="Chatbot Response"),
46
- gr.Audio(type="filepath", label="Voice Output")
47
- ],
48
- title="Real-Time Voice-to-Voice Chatbot",
49
- description="Speak to the chatbot and get a spoken response!",
50
  )
51
 
52
- # Launch Interface
53
- if __name__ == "__main__":
54
- interface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import os
2
+ from dotenv import load_dotenv
 
3
  from groq import Groq
4
+ import streamlit as st
 
5
 
6
+ # Load environment variables
7
+ load_dotenv()
8
 
9
+ # Initialize Groq client
10
+ client = Groq(
11
+ api_key=os.environ.get("GROQ_API_KEY"),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  )
13
 
14
+ # Streamlit UI
15
+ st.title("Python Bot with Groq's API")
16
+
17
+ # User input
18
+ user_input = st.text_input("Enter your question or message:")
19
+
20
+ # Submit button
21
+ if st.button("Submit"):
22
+ if user_input.strip():
23
+ # Interact with Groq's LLM
24
+ with st.spinner("Fetching response from the LLM..."):
25
+ try:
26
+ chat_completion = client.chat.completions.create(
27
+ messages=[
28
+ {"role": "user", "content": user_input},
29
+ ],
30
+ model="llama3-8b-8192",
31
+ stream=False,
32
+ )
33
+ response = chat_completion.choices[0].message.content
34
+ st.success("Response received:")
35
+ st.write(response)
36
+ except Exception as e:
37
+ st.error(f"Error: {e}")
38
+ else:
39
+ st.warning("Please enter a valid message!")
40
+
41
+ # Deployment instructions
42
+ st.markdown(
43
+ """
44
+ ### Deployment
45
+ - Install `streamlit` and `groq` locally.
46
+ - Run this app with `streamlit run your_script_name.py`.
47
+ - Push it to Hugging Face for wider accessibility.
48
+ """
49
+ )