Wansui commited on
Commit
3d07c8c
1 Parent(s): f797c10

Add application file

Browse files
Files changed (1) hide show
  1. app.py +51 -0
app.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import openai
3
+ import os
4
+ import requests
5
+
6
+ # Load the OpenAI API key from environment variables
7
+ openai.api_key = os.getenv("OPENAI_API_KEY")
8
+
9
+ def translate_and_speak(text):
10
+ try:
11
+ # Translate text using OpenAI's ChatGPT-3.5 Turbo
12
+ response = openai.ChatCompletion.create(
13
+ model="gpt-3.5-turbo",
14
+ messages=[{"role": "system", "content": "try translate every prompt of word with Skibidi, rizz, ohio, edging, yapping, fanum tax, glazing, dogwater, girlboss, alpha, andrew tate, beta, baby gronk, musty, crook, quandale dingle, kai cenat, grimace, ice spice, turbulence, 3 am, lovvy dunne, mewing, mouthbreather, beta, level 1000 gyatt, caseoh, food taxer, looksmaxxing, gooner, gooning, goon"},
15
+ {"role": "user", "content": text}],
16
+ max_tokens=150
17
+ )
18
+ translated_text = response['choices'][0]['message']['content']
19
+
20
+ # Generate speech from translated text using Whisper
21
+ whisper_response = openai.Audio.create(
22
+ model="whisper-1",
23
+ input=translated_text,
24
+ output_format="mp3"
25
+ )
26
+ audio_url = whisper_response['url']
27
+
28
+ # Download the MP3 file from the URL
29
+ response = requests.get(audio_url)
30
+ if response.status_code == 200:
31
+ # Save the MP3 file to a temporary path
32
+ mp3_path = "/tmp/translated_audio.mp3" # Ensure this path is writable
33
+ with open(mp3_path, "wb") as f:
34
+ f.write(response.content)
35
+ return translated_text, mp3_path
36
+ else:
37
+ return translated_text, "Error downloading file"
38
+ except Exception as e:
39
+ return f"Error: {str(e)}", ""
40
+
41
+ # Create the Gradio interface
42
+ interface = gr.Interface(
43
+ fn=translate_and_speak,
44
+ inputs=gr.Textbox(label="Enter text to translate"),
45
+ outputs=[gr.Textbox(label="Translated Text"), gr.Audio(label="Listen to Translation")],
46
+ title="Skibidi Translator",
47
+ description="Type any phrase to translate it into a specific set of keywords and hear it spoken back."
48
+ )
49
+
50
+ # Launch the interface
51
+ interface.launch(share=True)