Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import openai
|
| 3 |
+
import os
|
| 4 |
+
from pydub import AudioSegment
|
| 5 |
+
import numpy as np
|
| 6 |
+
|
| 7 |
+
# Set your OpenAI API key here
|
| 8 |
+
openai.api_key = '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'
|
| 9 |
+
|
| 10 |
+
def generate_audio(text):
|
| 11 |
+
"""Generate audio using OpenAI API"""
|
| 12 |
+
response = openai.Audio.create(
|
| 13 |
+
model="text-to-speech",
|
| 14 |
+
prompt=text
|
| 15 |
+
)
|
| 16 |
+
audio_file = response['audio']
|
| 17 |
+
return audio_file
|
| 18 |
+
|
| 19 |
+
def transcribe_audio(file):
|
| 20 |
+
"""Transcribe audio file to text"""
|
| 21 |
+
audio = AudioSegment.from_file(file)
|
| 22 |
+
audio_array = np.array(audio.get_array_of_samples())
|
| 23 |
+
# Transcribe using OpenAI API (pseudo code)
|
| 24 |
+
response = openai.Audio.transcribe(
|
| 25 |
+
model="whisper-1",
|
| 26 |
+
audio=audio_array.tolist()
|
| 27 |
+
)
|
| 28 |
+
return response['text']
|
| 29 |
+
|
| 30 |
+
def main():
|
| 31 |
+
st.title("Audio App with OpenAI API")
|
| 32 |
+
|
| 33 |
+
# Text-to-Audio section
|
| 34 |
+
st.header("Text to Audio")
|
| 35 |
+
user_text = st.text_area("Enter text:")
|
| 36 |
+
if st.button("Generate Audio"):
|
| 37 |
+
if user_text:
|
| 38 |
+
audio = generate_audio(user_text)
|
| 39 |
+
st.audio(audio, format='audio/wav')
|
| 40 |
+
|
| 41 |
+
# Audio-to-Text section
|
| 42 |
+
st.header("Audio to Text")
|
| 43 |
+
audio_file = st.file_uploader("Upload Audio File", type=["wav", "mp3", "ogg"])
|
| 44 |
+
if st.button("Transcribe Audio"):
|
| 45 |
+
if audio_file is not None:
|
| 46 |
+
transcription = transcribe_audio(audio_file)
|
| 47 |
+
st.write("Transcription:")
|
| 48 |
+
st.write(transcription)
|
| 49 |
+
|
| 50 |
+
if __name__ == "__main__":
|
| 51 |
+
main()
|