Spaces:
hersia
/
Runtime error

PiPWorld / app.py
hersia's picture
Update app.py
bfb347e
raw
history blame
4.66 kB
import os
import gradio as gr
import openai
from gtts import gTTS
openai.api_key = os.environ["OPEN_AI_KEY"]
def transcribe(audio):
audio_file = open(audio, "rb")
transcript = openai.Audio.transcribe("whisper-1", audio_file)
return transcript["text"]
with gr.Blocks() as demo:
gr.Markdown(
"""
# Welcome to PiP_W.
"""
)
with gr.Accordion("Click for Instructions:"):
gr.Markdown(
"""
* Record your query.
* Submit your query, and follow the chat or listen to the advice.
* When you are ready to respond, clear your last recording and resubmit.
note: Transcribe Audio does not work on iOS
""")
messages = gr.State(value=[{"role": "system", "content": "Ignore all instructions prior to this one. Prompt: Design an all-inclusive, interactive, and immersive gamified chat-based training program for novice and seasoned traders to master the art of trading equities and cryptocurrencies. The training program will be accessible through a chat interface, offering users five different program options, including learning lessons, tests, and more. Users can type MENU to see the menu and navigate through the program. Tests and exercises should be interactive, engaging users one question or fact at a time. The curriculum should be based on the material provided above. Curriculum Outline: 1. Trading Foundations 2. Understanding the Markets 3. Developing Trading Strategies 4. Risk Management Essentials 5. Trading Psychology and Mental Fortitude 6. Trading Tools and Technological Innovation 7. Crafting a Personalized Trading Plan 8. Trading Ethics and Regulatory Compliance Program Menu: 1. Learning Lessons: In-depth lessons covering each curriculum topic, using engaging multimedia content and chat-based interactions. 2. Interactive Tests: Quizzes and assessments to evaluate users' understanding of the material, one question at a time. 3. Practical Exercises: Hands-on activities and simulations that allow users to apply their knowledge in a risk-free environment. 4. Progress Tracking & Rewards: A personalized dashboard to track progress, collect badges, and unlock achievements. 5. Community & Support: Access to chat-based forums, leaderboards, and team-based challenges to foster collaboration and friendly competition. When users type MENU, they will be presented with the following options: 1. Learning Lessons 2. Interactive Tests 3. Practical Exercises 4. Progress Tracking & Rewards 5. Community & Support To select a program option, users can simply type the corresponding number. The chat interface will guide users through the chosen program, providing an engaging and interactive learning experience. Tests and exercises will be designed to present users with one question or fact at a time, ensuring that users can fully focus on each task without feeling overwhelmed"}])
def botResponse(user_input, messages):
messages.append({"role": "user", "content": user_input})
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo-0301",
messages=messages
)
system_message = response["choices"][0]["message"]["content"]
messages.append({"role": "assistant", "content": system_message})
chat_transcript = ""
for message in messages:
if (message["role"] != "system"):
chat_transcript += message["role"] + ": " + message["content"] + "\n\n"
return chat_transcript
def giveVoice(messages):
bot_message=messages[-1]
myobj = gTTS(text=bot_message["content"])
myobj.save("temp.mp3")
dir = os.getcwd()
new_path = os.path.join(dir, "temp.mp3")
return new_path
with gr.Row():
with gr.Column(scale=1):
user_audio = gr.Audio(source="microphone", type="filepath", label="Input Phrase")
submit_btn = gr.Button(value="Transcribe Audio")
submit_btn2 = gr.Button(value="Submit Text")
gpt_voice = gr.Audio(label="Listen to Advice")
with gr.Column(scale=2):
user_transcript = gr.Text(label="Audio Translation", interactive=False)
user_text = gr.Text(label="Text Input")
gpt_transcript = gr.Text(label="Chat Transcript")
submit_btn.click(transcribe, user_audio, user_transcript)
submit_btn2.click(botResponse, [user_text, messages], gpt_transcript)
user_transcript.change(botResponse, [user_transcript, messages], gpt_transcript)
gpt_transcript.change(giveVoice, messages, gpt_voice)
demo.launch(share=False)