updated api key
Browse files
app.py
CHANGED
@@ -1,63 +1,67 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
)
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
for message in client.chat_completion(
|
31 |
-
messages,
|
32 |
-
max_tokens=max_tokens,
|
33 |
-
stream=True,
|
34 |
-
temperature=temperature,
|
35 |
-
top_p=top_p,
|
36 |
-
):
|
37 |
-
token = message.choices[0].delta.content
|
38 |
-
|
39 |
-
response += token
|
40 |
-
yield response
|
41 |
-
|
42 |
-
"""
|
43 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
44 |
-
"""
|
45 |
-
demo = gr.ChatInterface(
|
46 |
-
respond,
|
47 |
-
additional_inputs=[
|
48 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
49 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
50 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
51 |
-
gr.Slider(
|
52 |
-
minimum=0.1,
|
53 |
-
maximum=1.0,
|
54 |
-
value=0.95,
|
55 |
-
step=0.05,
|
56 |
-
label="Top-p (nucleus sampling)",
|
57 |
-
),
|
58 |
-
],
|
59 |
-
)
|
60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
-
|
63 |
-
|
|
|
1 |
+
import os
|
2 |
import gradio as gr
|
3 |
+
import whisper
|
4 |
+
from gtts import gTTS
|
5 |
+
import io
|
6 |
+
import almlapi # Assuming AL/ML has a Python API library
|
7 |
+
|
8 |
+
# Set your AL/ML API key for authentication
|
9 |
+
os.environ["ALML_API_KEY"] = "701b35863e6d4a7b81bdcad2e6f3c880"
|
10 |
+
|
11 |
+
# Load the Whisper model for audio transcription
|
12 |
+
model = whisper.load_model("base")
|
13 |
+
|
14 |
+
# Function to process audio and interact with the AL/ML API
|
15 |
+
def process_audio(file_path):
|
16 |
+
try:
|
17 |
+
# Load and transcribe audio using Whisper
|
18 |
+
audio = whisper.load_audio(file_path)
|
19 |
+
result = model.transcribe(audio)
|
20 |
+
text = result["text"]
|
21 |
+
|
22 |
+
# Call OpenAI o1 model via AL/ML API for problem-solving
|
23 |
+
response = almlapi.call_o1(
|
24 |
+
api_key=os.environ.get("ALML_API_KEY"),
|
25 |
+
prompt=text,
|
26 |
+
model="o1" # Model name, adjust according to AL/ML documentation
|
27 |
+
)
|
28 |
+
|
29 |
+
# Extract the response message
|
30 |
+
response_message = response["generated_text"].strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
+
# Convert response message to speech using gTTS
|
33 |
+
tts = gTTS(response_message)
|
34 |
+
response_audio_io = io.BytesIO()
|
35 |
+
tts.write_to_fp(response_audio_io) # Save the audio to BytesIO object
|
36 |
+
response_audio_io.seek(0)
|
37 |
+
|
38 |
+
# Save the audio file
|
39 |
+
with open("response.mp3", "wb") as audio_file:
|
40 |
+
audio_file.write(response_audio_io.getvalue())
|
41 |
+
|
42 |
+
# Return the response text and audio file path
|
43 |
+
return response_message, "response.mp3"
|
44 |
+
|
45 |
+
except Exception as e:
|
46 |
+
# Handle exceptions
|
47 |
+
return f"An error occurred: {e}", None
|
48 |
+
|
49 |
+
# Interface configurations (UI)
|
50 |
+
title = "Voice-to-Voice AI Chatbot with AL/ML API"
|
51 |
+
description = "Developed by [Adnan Tariq](https://www.linkedin.com/in/adnaantariq/) with ❤️"
|
52 |
+
article = "### Instructions\n1. Upload an audio file.\n2. Wait for the transcription.\n3. Listen to the chatbot's response."
|
53 |
+
|
54 |
+
# Gradio interface setup
|
55 |
+
iface = gr.Interface(
|
56 |
+
fn=process_audio,
|
57 |
+
inputs=gr.Audio(type="filepath"), # Upload audio via file path
|
58 |
+
outputs=[gr.Textbox(label="Response Text"), gr.Audio(label="Response Audio")],
|
59 |
+
live=True,
|
60 |
+
title=title,
|
61 |
+
description=description,
|
62 |
+
theme="dark",
|
63 |
+
article=article
|
64 |
+
)
|
65 |
|
66 |
+
# Launch the Gradio app
|
67 |
+
iface.launch()
|