AI-Edify commited on
Commit
112a3e2
·
verified ·
1 Parent(s): b348d8d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -32
app.py CHANGED
@@ -1,51 +1,31 @@
1
  import os
2
  import gradio as gr
3
  import openai
4
- from openai import OpenAI
5
  import speech_recognition as sr
6
- import threading
7
  import time
8
 
9
- # Initialize OpenAI client with API key from environment variable
10
- client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
11
-
12
- # Create an assistant
13
- assistant = client.beta.assistants.create(
14
- name="Pronunciation Assistant",
15
- instructions="You are a helpful pronunciation assistant. You compare the generated text with the user's transcription and then provide feedback on how the user can improve their pronunciation accordingly. You also single out specific words they pronounced incorrectly and give tips on how to improve like for example 'schedule' can be pronounced as 'sked-jool'.",
16
- model="gpt-4-1106-preview"
17
- )
18
 
19
  def generate_text():
20
- response = client.chat.completions.create(
21
  model="gpt-3.5-turbo",
22
  messages=[
23
  {"role": "system", "content": "Generate a short paragraph (2-3 sentences) for an English learner to read aloud."},
24
  {"role": "user", "content": "Create a practice text."}
25
  ]
26
  )
27
- return response.choices[0].message.content
28
 
29
  def get_pronunciation_feedback(original_text, transcription):
30
- thread = client.beta.threads.create()
31
-
32
- message = client.beta.threads.messages.create(
33
- thread_id=thread.id,
34
- role="user",
35
- content=f"Original text: '{original_text}'\nTranscription: '{transcription}'\nProvide pronunciation feedback."
36
- )
37
-
38
- run = client.beta.threads.runs.create(
39
- thread_id=thread.id,
40
- assistant_id=assistant.id
41
  )
42
-
43
- while run.status != "completed":
44
- time.sleep(1)
45
- run = client.beta.threads.runs.retrieve(thread_id=thread.id, run_id=run.id)
46
-
47
- messages = client.beta.threads.messages.list(thread_id=thread.id)
48
- return messages.data[0].content[0].text.value
49
 
50
  def transcribe_audio_realtime(audio):
51
  recognizer = sr.Recognizer()
@@ -68,7 +48,7 @@ def practice_pronunciation(audio):
68
  demo = gr.Interface(
69
  fn=practice_pronunciation,
70
  inputs=[
71
- gr.Audio(type="filepath") # Removed 'source="microphone"'
72
  ],
73
  outputs=[
74
  gr.Textbox(label="Text to Read"),
 
1
  import os
2
  import gradio as gr
3
  import openai
 
4
  import speech_recognition as sr
 
5
  import time
6
 
7
+ # Set OpenAI API key
8
+ openai.api_key = os.environ.get("OPENAI_API_KEY")
 
 
 
 
 
 
 
9
 
10
  def generate_text():
11
+ response = openai.ChatCompletion.create(
12
  model="gpt-3.5-turbo",
13
  messages=[
14
  {"role": "system", "content": "Generate a short paragraph (2-3 sentences) for an English learner to read aloud."},
15
  {"role": "user", "content": "Create a practice text."}
16
  ]
17
  )
18
+ return response.choices[0].message['content']
19
 
20
  def get_pronunciation_feedback(original_text, transcription):
21
+ response = openai.ChatCompletion.create(
22
+ model="gpt-3.5-turbo",
23
+ messages=[
24
+ {"role": "system", "content": "You are a helpful pronunciation assistant. Compare the generated text with the user's transcription and provide feedback on how the user can improve their pronunciation. Single out specific words they pronounced incorrectly and give tips on how to improve, like for example 'schedule' can be pronounced as 'sked-jool'."},
25
+ {"role": "user", "content": f"Original text: '{original_text}'\nTranscription: '{transcription}'\nProvide pronunciation feedback."}
26
+ ]
 
 
 
 
 
27
  )
28
+ return response.choices[0].message['content']
 
 
 
 
 
 
29
 
30
  def transcribe_audio_realtime(audio):
31
  recognizer = sr.Recognizer()
 
48
  demo = gr.Interface(
49
  fn=practice_pronunciation,
50
  inputs=[
51
+ gr.Audio(type="filepath")
52
  ],
53
  outputs=[
54
  gr.Textbox(label="Text to Read"),