-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp3.py
More file actions
79 lines (61 loc) · 2.13 KB
/
app3.py
File metadata and controls
79 lines (61 loc) · 2.13 KB
1
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import gradio as gr
# from openai import OpenAI
import openai
from langchain.llms import OpenAI
import os
from dotenv import load_dotenv
import subprocess # To run the whisper command
from gtts import gTTS
from io import BytesIO
import tempfile
load_dotenv()
def process(filepath):
# set openai key
client = openai.OpenAI(api_key="your-key")
my_key = "your-key"
# Store the voice input to a file
audio = open(str(filepath), "rb")
# openai speech-to-text
transcript =client.audio.transcriptions.create(
model="whisper-1",
file=audio)
# langchain
llm = OpenAI(temperature=1, openai_api_key = my_key)
# Construct the prompt for translation
prompt_for_translation = f"Translate this to Japanese: {transcript}"
# Get the translation response
translation_response = llm(prompt_for_translation)
# Convert translated text to speech
tts = gTTS(translation_response, lang='ja') # 'ja' is the language code for Japanese
mp3_fp = BytesIO()
tts.write_to_fp(mp3_fp)
mp3_fp.seek(0) # Rewind the file
# Convert translated text to speech
tts = gTTS(translation_response, lang='ja')
mp3_fp = BytesIO()
tts.write_to_fp(mp3_fp)
mp3_fp.seek(0) # Rewind the file
# Convert BytesIO to bytes
audio_bytes = mp3_fp.getvalue()
tts = gTTS(translation_response, lang='ja') # 'ja' is the language code for Japanese
# Save to a temporary file
with tempfile.NamedTemporaryFile(delete=False, suffix='.mp3') as temp_audio_file:
tts.save(temp_audio_file.name)
return temp_audio_file.name # Return the path of the temp file
return audio_bytes
# Return the audio file
#return mp3_fp
#return translation_response
#
# Update the outputs in your Gradio Interface to return audio
demo = gr.Interface(
fn=process,
inputs=gr.Audio(sources="microphone", type="filepath"),
outputs=gr.Audio()
)
# demo = gr.Interface(
# fn=process,
# inputs=gr.Audio(sources="microphone", type="filepath"),
# outputs="text"
#)
demo.launch()