simonraj commited on
Commit
067ad34
·
verified ·
1 Parent(s): fa5008a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -40
app.py CHANGED
@@ -49,9 +49,9 @@ def transcribe(audio_path):
49
 
50
  # Inference function using Hugging Face InferenceClient
51
  @spaces.GPU(duration=120)
52
- def model(text):
53
  system_instructions = (
54
- "[SYSTEM] You are OralCoach. Provide feedback, bias conciseness "
55
  )
56
  generate_kwargs = dict(
57
  temperature=0.7,
@@ -61,7 +61,7 @@ def model(text):
61
  do_sample=True,
62
  seed=42,
63
  )
64
- formatted_prompt = system_instructions + text + "[OralCoach]"
65
  stream = client.text_generation(
66
  formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
67
  output = ""
@@ -71,6 +71,7 @@ def model(text):
71
  return {"choices": [{"delta": {"content": output}}]}
72
 
73
 
 
74
  # Text-to-Speech function using edge_tts
75
  async def generate_audio_feedback(feedback_text):
76
  communicate = edge_tts.Communicate(feedback_text)
@@ -87,48 +88,50 @@ async def generate_audio_feedback(feedback_text):
87
  return None
88
  return tmp_path
89
 
90
-
91
- # Generating feedback for the Oral Coach
92
  async def generate_feedback(user_id, question_choice, strategy_choice, message, feedback_level):
93
  current_question_index = thinkingframes.questions.index(question_choice)
94
  strategy, explanation = thinkingframes.strategy_options[strategy_choice]
95
 
96
- conversation = [{
97
  "role": "system",
98
- "content": f"You are an expert Primary 6 English Language Teacher in a Singapore Primary school, "
99
- f"directly guiding a Primary 6 student in Singapore in their oral responses. "
100
- f"Format the feedback in Markdown so that it can be easily read. "
101
- f"Address the student directly in the second person in your feedback. "
102
- f"The student is answering the question: '{thinkingframes.questions[current_question_index]}'. "
103
- f"For Question 1, consider the picture description: '{thinkingframes.description}'. "
104
- f"For Questions 2 and 3, the picture is not relevant, so the student should not refer to it in their response. "
105
- f"Analyze the student's response using the following step-by-step approach: "
106
- f"1. Evaluate the response against the {strategy} thinking frame. "
107
- f"2. Assess how well the student's response addresses each criteria of the {strategy} thinking frame: "
108
- f" - Assign emoticon scores based on how well the student comprehensively covered each criteria: "
109
- f" - 😊😊😊 (three smiling faces) for a good coverage "
110
- f" - 😊😊 (two smiling faces) for an average coverage "
111
- f" - 😊 (one smiling face) for a poor coverage "
112
- f" - Provide a clear, direct, and concise explanation of how well the answer addresses each criteria. "
113
- f" - Identify specific areas for improvement in students responses, and provide targeted suggestions for improvement. "
114
- f"3. Identify overall strengths and areas for improvement in the student's response using the {strategy} to format and provide targeted areas for improvement. "
115
- f"4. Provide specific feedback on grammar, vocabulary, and sentence structure. "
116
- f" Suggest age-appropriate enhancements that are one level higher than the student's current response. "
117
- f"5. Conclude with follow-up questions for reflection. "
118
- f"If the student's response deviates from the question, provide clear and concise feedback to help them refocus and try again. "
119
- f"Ensure that the vocabulary and sentence structure recommendations are achievable for Primary 6 students in Singapore. "
120
- f"Example Feedback Structure for Each Criteria: "
121
- f"Criteria: [Criteria Name] "
122
- f"Score: [Smiling emoticons] "
123
- f"Explanation: [Clear, direct, and concise explanation of how well the answer addresses the criteria. Identify specific areas for improvement, and provide targeted suggestions for improvement.] "
124
- f"{thinkingframes.generate_prompt(feedback_level)}"
125
- }, {
126
- "role": "user",
127
- "content": message
128
- }]
129
-
130
- user_message = conversation[1]["content"] # Extract the user message from the conversation
131
- response = model(user_message)
 
 
 
132
 
133
  chat_history = [] # Initialize chat history outside the loop
134
  full_feedback = "" # Accumulate the entire feedback message
@@ -136,6 +139,7 @@ async def generate_feedback(user_id, question_choice, strategy_choice, message,
136
  for chunk in response["choices"]:
137
  if chunk["delta"] and chunk["delta"]["content"]:
138
  feedback_chunk = chunk["delta"]["content"]
 
139
  yield feedback_chunk # Yield each feedback chunk as it is generated
140
  await asyncio.sleep(0)
141
 
 
49
 
50
  # Inference function using Hugging Face InferenceClient
51
  @spaces.GPU(duration=120)
52
+ def model(conversation):
53
  system_instructions = (
54
+ "[SYSTEM] You are OralCoach, an AI-powered conversational coach. Guide the student through their oral responses "
55
  )
56
  generate_kwargs = dict(
57
  temperature=0.7,
 
61
  do_sample=True,
62
  seed=42,
63
  )
64
+ formatted_prompt = "\n".join([f"{msg['role'].upper()}: {msg['content']}" for msg in conversation])
65
  stream = client.text_generation(
66
  formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
67
  output = ""
 
71
  return {"choices": [{"delta": {"content": output}}]}
72
 
73
 
74
+
75
  # Text-to-Speech function using edge_tts
76
  async def generate_audio_feedback(feedback_text):
77
  communicate = edge_tts.Communicate(feedback_text)
 
88
  return None
89
  return tmp_path
90
 
91
+ #generate feedback
 
92
  async def generate_feedback(user_id, question_choice, strategy_choice, message, feedback_level):
93
  current_question_index = thinkingframes.questions.index(question_choice)
94
  strategy, explanation = thinkingframes.strategy_options[strategy_choice]
95
 
96
+ system_instructions = {
97
  "role": "system",
98
+ "content": (
99
+ f"You are an expert Primary 6 English Language Teacher in a Singapore Primary school, "
100
+ f"directly guiding a Primary 6 student in Singapore in their oral responses. "
101
+ f"Format the feedback in Markdown so that it can be easily read. "
102
+ f"Address the student directly in the second person in your feedback. "
103
+ f"The student is answering the question: '{thinkingframes.questions[current_question_index]}'. "
104
+ f"For Question 1, consider the picture description: '{thinkingframes.description}'. "
105
+ f"For Questions 2 and 3, the picture is not relevant, so the student should not refer to it in their response. "
106
+ f"Analyze the student's response using the following step-by-step approach: "
107
+ f"1. Evaluate the response against the {strategy} thinking frame. "
108
+ f"2. Assess how well the student's response addresses each criteria of the {strategy} thinking frame: "
109
+ f" - Assign emoticon scores based on how well the student comprehensively covered each criteria: "
110
+ f" - 😊😊😊 (three smiling faces) for a good coverage "
111
+ f" - 😊😊 (two smiling faces) for an average coverage "
112
+ f" - 😊 (one smiling face) for a poor coverage "
113
+ f" - Provide a clear, direct, and concise explanation of how well the answer addresses each criteria. "
114
+ f" - Identify specific areas for improvement in students responses, and provide targeted suggestions for improvement. "
115
+ f"3. Identify overall strengths and areas for improvement in the student's response using the {strategy} to format and provide targeted areas for improvement. "
116
+ f"4. Provide specific feedback on grammar, vocabulary, and sentence structure. "
117
+ f" Suggest age-appropriate enhancements that are one level higher than the student's current response. "
118
+ f"5. Conclude with follow-up questions for reflection. "
119
+ f"If the student's response deviates from the question, provide clear and concise feedback to help them refocus and try again. "
120
+ f"Ensure that the vocabulary and sentence structure recommendations are achievable for Primary 6 students in Singapore. "
121
+ f"Example Feedback Structure for Each Criteria: "
122
+ f"Criteria: [Criteria Name] "
123
+ f"Score: [Smiling emoticons] "
124
+ f"Explanation: [Clear, direct, and concise explanation of how well the answer addresses the criteria. Identify specific areas for improvement, and provide targeted suggestions for improvement.] "
125
+ f"{thinkingframes.generate_prompt(feedback_level)}"
126
+ )
127
+ }
128
+
129
+ conversation = [
130
+ system_instructions,
131
+ {"role": "user", "content": message}
132
+ ]
133
+
134
+ response = model(conversation)
135
 
136
  chat_history = [] # Initialize chat history outside the loop
137
  full_feedback = "" # Accumulate the entire feedback message
 
139
  for chunk in response["choices"]:
140
  if chunk["delta"] and chunk["delta"]["content"]:
141
  feedback_chunk = chunk["delta"]["content"]
142
+ full_feedback += feedback_chunk
143
  yield feedback_chunk # Yield each feedback chunk as it is generated
144
  await asyncio.sleep(0)
145