ankban commited on
Commit
35002f0
Β·
verified Β·
1 Parent(s): 07cd64d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -21
app.py CHANGED
@@ -9,9 +9,9 @@ from openai import OpenAI
9
 
10
  # Use writable cache for Hugging Face
11
  os.environ["MPLCONFIGDIR"] = "/tmp/matplotlib"
12
- os.environ["HF_HOME"] = "/data/hf"
13
- os.environ["TRANSFORMERS_CACHE"] = "/data/hf"
14
- os.environ["XDG_CACHE_HOME"] = "/data/hf"
15
 
16
  chatter_owl_url = "file/images/chatter_owl.png"
17
 
@@ -20,28 +20,29 @@ model = WhisperModel("base", compute_type="int8") # Fastest CPU option
20
  openai.api_key = os.getenv("OPENAI_API_KEY")
21
 
22
  # Prompt template
23
- PROMPT_TEMPLATE = f"""
24
  You are a communication coach evaluating a user's spoken response.
25
 
26
- Please evaluate their speech using the following **five dimensions**:
27
  1. Clarity
28
  2. Structure
29
  3. Fluency
30
  4. Content Relevance
31
  5. Tone & Expression
32
 
33
- For each category:
34
- - Give a score from 0 to 10
35
- - Provide a brief but detailed explanation
36
 
37
  Then:
38
- - Summarize their overall performance in 3–4 lines
39
- - End with one motivational sentence
40
 
41
- Make sure your tone is constructive and supportive, even if the scores are low.
 
42
 
43
  Transcript:
44
- {transcript}
45
  """
46
 
47
  # Convert uploaded audio to 16kHz mono WAV
@@ -98,6 +99,29 @@ def tutor_feedback(audio_file):
98
 
99
  return transcript, feedback_text, mp3_path
100
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
  # Gradio interface
102
  # Path to your CSS file
103
  css_path = "light_mode_chatter_owl.css" # Make sure this file is in the same directory as app.py
@@ -105,13 +129,14 @@ css_path = "light_mode_chatter_owl.css" # Make sure this file is in the same di
105
  # Path to your CSS file
106
  css_path = "light_mode_chatter_owl.css" # Make sure this file is in the same directory as app.py
107
 
108
- with gr.Blocks(css=css_path) as app:
 
109
  gr.Markdown(
110
  """
111
  <div id="header" style="text-align: center;">
112
- <img src="file/images/chatter_owl.png">
113
- <h2>πŸ¦‰ Meet Chatter the Owl!</h2>
114
- <p>Speak to me and I'll help you become a confident communicator. 🎀✨</p>
115
  </div>
116
  """
117
  )
@@ -119,14 +144,26 @@ with gr.Blocks(css=css_path) as app:
119
  with gr.Row():
120
  audio_input = gr.Audio(type="filepath", label="πŸŽ™ Speak or Upload Audio")
121
 
 
 
 
 
 
122
  with gr.Row():
123
- transcript_box = gr.Textbox(label="πŸ“– What You Said")
124
- feedback_box = gr.Textbox(label="πŸ’‘ Chatter's Feedback")
 
 
 
 
 
 
 
 
 
125
 
126
- audio_output = gr.Audio(label="πŸ”Š Spoken Feedback", type="filepath")
127
 
128
- # Hook up your function (e.g., `tutor_feedback`)
129
- audio_input.change(fn=tutor_feedback, inputs=audio_input, outputs=[transcript_box, feedback_box, audio_output])
130
 
131
  if __name__ == "__main__":
132
  print("βœ… App is launching...")
 
9
 
10
  # Use writable cache for Hugging Face
11
  os.environ["MPLCONFIGDIR"] = "/tmp/matplotlib"
12
+ os.environ["HF_HOME"] = "/tmp/hf"
13
+ os.environ["TRANSFORMERS_CACHE"] = "/tmp/hf"
14
+ os.environ["XDG_CACHE_HOME"] = "/tmp/hf"
15
 
16
  chatter_owl_url = "file/images/chatter_owl.png"
17
 
 
20
  openai.api_key = os.getenv("OPENAI_API_KEY")
21
 
22
  # Prompt template
23
+ PROMPT_TEMPLATE = """
24
  You are a communication coach evaluating a user's spoken response.
25
 
26
+ Evaluate the speech using these 5 dimensions:
27
  1. Clarity
28
  2. Structure
29
  3. Fluency
30
  4. Content Relevance
31
  5. Tone & Expression
32
 
33
+ For each:
34
+ - Score from 0–10
35
+ - Provide brief, specific feedback
36
 
37
  Then:
38
+ - Give an overall 3–4 line summary
39
+ - End with a motivational sentence
40
 
41
+ Lastly:
42
+ - Provide an improved version of the speech that better demonstrates effective communication, based on their original transcript.
43
 
44
  Transcript:
45
+ \"\"\"{transcript}\"\"\"
46
  """
47
 
48
  # Convert uploaded audio to 16kHz mono WAV
 
99
 
100
  return transcript, feedback_text, mp3_path
101
 
102
+ # Generate example version of the same speech
103
+ def generate_example_response(transcript):
104
+ prompt = f"""
105
+ You are a communication tutor. Rewrite this speech transcript to be a more polished and confident version, while keeping the meaning and tone similar.
106
+
107
+ Transcript:
108
+ {transcript}
109
+ """
110
+ response = openai.ChatCompletion.create(
111
+ model="gpt-4",
112
+ messages=[
113
+ {"role": "system", "content": "You are a helpful speaking coach."},
114
+ {"role": "user", "content": prompt}
115
+ ],
116
+ temperature=0.7
117
+ )
118
+ return response["choices"][0]["message"]["content"]
119
+
120
+ # Hook up logic
121
+ def show_example_feedback(transcript):
122
+ # Extract only example portion from GPT or return a pre-generated version
123
+ return generate_example_response(transcript)
124
+
125
  # Gradio interface
126
  # Path to your CSS file
127
  css_path = "light_mode_chatter_owl.css" # Make sure this file is in the same directory as app.py
 
129
  # Path to your CSS file
130
  css_path = "light_mode_chatter_owl.css" # Make sure this file is in the same directory as app.py
131
 
132
+ # App UI
133
+ with gr.Blocks(css="light_mode_chatter_owl.css") as app:
134
  gr.Markdown(
135
  """
136
  <div id="header" style="text-align: center;">
137
+ <img src="file/images/chatter_owl.png" width="120">
138
+ <h2>πŸ¦‰ Meet <strong>Chatter the Owl</strong></h2>
139
+ <p>Speak into the mic, and I’ll give you structured feedback to help you grow as a communicator!</p>
140
  </div>
141
  """
142
  )
 
144
  with gr.Row():
145
  audio_input = gr.Audio(type="filepath", label="πŸŽ™ Speak or Upload Audio")
146
 
147
+ transcript_box = gr.Textbox(label="πŸ“– What You Said", interactive=False)
148
+ feedback_box = gr.Textbox(label="πŸ’‘ Chatter’s Feedback", interactive=False)
149
+ audio_output = gr.Audio(label="πŸ”Š Chatter Speaks", type="filepath")
150
+ hidden_transcript = gr.Textbox(visible=False)
151
+
152
  with gr.Row():
153
+ try_again = gr.Button("πŸ” Try Again")
154
+ show_example = gr.Button("🎯 Show Me an Example")
155
+
156
+ example_box = gr.Textbox(label="πŸ—£ Suggested Improvement", visible=False)
157
+
158
+ # Interactions
159
+ audio_input.change(fn=tutor_feedback, inputs=audio_input,
160
+ outputs=[transcript_box, feedback_box, audio_output, hidden_transcript])
161
+
162
+ try_again.click(fn=lambda: ("", "", None, "", ""), inputs=None,
163
+ outputs=[transcript_box, feedback_box, audio_output, hidden_transcript, example_box])
164
 
165
+ show_example.click(fn=generate_example_response, inputs=hidden_transcript, outputs=example_box)
166
 
 
 
167
 
168
  if __name__ == "__main__":
169
  print("βœ… App is launching...")