File size: 7,614 Bytes
cf815ef
 
 
 
 
 
 
 
 
306781b
cf815ef
306781b
cf815ef
 
306781b
 
 
 
d5a7196
306781b
cf815ef
017ac29
 
 
 
 
 
 
 
 
cf815ef
 
 
 
21259cf
345a5f2
cf815ef
 
 
 
345a5f2
017ac29
 
 
 
 
 
345a5f2
21259cf
 
 
 
cf815ef
 
 
21259cf
cf815ef
 
 
 
 
345a5f2
cf815ef
 
21259cf
cf815ef
21259cf
cf815ef
21259cf
bd675c8
21259cf
 
 
bd675c8
21259cf
cf815ef
bd675c8
21259cf
cf815ef
 
bd675c8
21259cf
cf815ef
 
 
bd675c8
cf815ef
 
 
 
 
 
345a5f2
cf815ef
 
bd675c8
cf815ef
 
bd675c8
cf815ef
21259cf
cf815ef
345a5f2
cf815ef
bd675c8
cf815ef
345a5f2
cf815ef
bd675c8
21259cf
 
bd675c8
cf815ef
 
 
bd675c8
cf815ef
21259cf
 
 
 
 
 
 
 
 
0233952
21259cf
cf815ef
 
 
 
 
21259cf
cf815ef
 
 
21259cf
cf815ef
21259cf
 
cf815ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21259cf
cf815ef
 
21259cf
 
 
 
 
cf815ef
 
 
 
 
 
 
 
 
0233952
d5a7196
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import gradio as gr
import os
import thinkingframes
import soundfile as sf
import numpy as np
import logging
from dotenv import load_dotenv
from policy import user_acceptance_policy
from styles import theme
from thinkingframes import generate_prompt, strategy_options, questions
from utils import get_image_html, collect_student_info
from database_functions import add_user_privacy, add_submission
from tab_teachers_dashboard import create_teachers_dashboard_tab
from config import CLASS_OPTIONS
import spaces
import edge_tts
import tempfile

# Load environment variables
load_dotenv()

# Whisper API settings
API_URL = "https://api-inference.huggingface.co/models/whisper-large"
headers = {"Authorization": f"Bearer {os.getenv('HF_AUTH_TOKEN')}"}

def whisper_query(filename):
    with open(filename, "rb") as f:
        data = f.read()
    response = requests.post(API_URL, headers=headers, data=data)
    return response.json()

# For maintaining user session (to keep track of userID)
user_state = gr.State(value="")

# Load the Meta-Llama-3-8B model from Hugging Face
llm = gr.load("meta-llama/Meta-Llama-3-8B", src="models")

image_path = "picturePerformance.jpg"
img_html = get_image_html(image_path)

@spaces.GPU(duration=120)
def transcribe(audio_path):
    response = whisper_query(audio_path)
    if "text" in response:
        return response["text"]
    else:
        raise ValueError("Transcription failed.")

@spaces.GPU(duration=120)
def generate_feedback(user_id, question_choice, strategy_choice, message, feedback_level):
    current_question_index = questions.index(question_choice)
    strategy, explanation = strategy_options[strategy_choice]

    conversation = [{
        "role": "system",
        "content": thinkingframes.generate_system_message(current_question_index, feedback_level)
    }, {
        "role": "user",
        "content": message
    }]

    feedback = llm(conversation)[0]["generated_text"]

    questionNo = current_question_index + 1
    add_submission(user_id, message, feedback, int(0), "", questionNo)

    return feedback

@spaces.GPU(duration=60)
def generate_audio_feedback(feedback_buffer):
    communicate = edge_tts.Communicate(feedback_buffer)
    with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
        tmp_path = tmp_file.name
        asyncio.run(communicate.save(tmp_path))
    return tmp_path

def predict(question_choice, strategy_choice, feedback_level, audio):
    current_audio_output = None

    if audio is None:
        return [("Oral Coach ⚡ϞϞ(๑⚈ ․̫ ⚈๑)∩ ⚡", "No audio data received. Please try again.")], current_audio_output

    sample_rate, audio_data = audio

    if audio_data is None or len(audio_data) == 0:
        return [("Oral Coach ⚡ϞϞ(๑⚈ ․̫ ⚈๑)∩ ⚡", "No audio data received. Please try again.")], current_audio_output

    audio_path = "audio.wav"
    if not isinstance(audio_data, np.ndarray):
        raise ValueError("audio_data must be a numpy array")
    sf.write(audio_path, audio_data, sample_rate)

    chat_history = [("Oral Coach ⚡ϞϞ(๑⚈ ․̫ ⚈๑)∩ ⚡", "Transcribing your audio, please listen to your oral response while waiting ...")]

    try:
        student_response = transcribe(audio_path)

        if not student_response.strip():
            return [("Oral Coach ⚡ϞϞ(๑⚈ ․̫ ⚈๑)∩ ⚡", "Transcription failed. Please try again or seek assistance.")], current_audio_output

        chat_history.append(("Student", student_response))

        chat_history.append(("Oral Coach ⚡ϞϞ(๑⚈ ․̫ ⚈๑)∩ ⚡", "Transcription complete. Generating feedback. Please continue listening to your oral response while waiting ..."))

        feedback = generate_feedback(int(user_state.value), question_choice, strategy_choice, student_response, feedback_level)

        chat_history.append(("Oral Coach ⚡ϞϞ(๑⚈ ․̫ ⚈๑)∩ ⚡", feedback))

        audio_output_path = generate_audio_feedback(feedback)

        current_audio_output = (24000, audio_output_path)
        return chat_history, current_audio_output

    except Exception as e:
        logging.error(f"An error occurred: {str(e)}", exc_info=True)
        return [("Oral Coach ⚡ϞϞ(๑⚈ ․̫ ⚈๑)∩ ⚡", "An error occurred. Please try again or seek assistance.")], current_audio_output

def toggle_oral_coach_visibility(class_name, index_no, policy_checked):
    if not policy_checked:
        return "Please agree to the Things to Note When using the Oral Coach ⚡ϞϞ(๑⚈ ․̫ ⚈๑)∩ ⚡ before submitting.", gr.update(visible=False)
    user_id, message = add_user_privacy(class_name, index_no)
    if "Error" in message:
        return message, gr.update(visible=False)
    user_state.value = user_id
    return message, gr.update(visible=True)

with gr.Blocks(title="Oral Coach powered by ZeroGPU⚡ϞϞ(๑⚈ ․̫ ⚈๑)∩ ⚡ and Meta AI 🦙 (LLama3)", theme=theme, css="footer {visibility: hidden}textbox{resize:none}") as demo:
    with gr.Tab("Oral Coach ⚡ϞϞ(๑⚈ ․̫ ⚈๑)∩ ⚡"):
        gr.Markdown("## Student Information")
        class_name = gr.Dropdown(label="Class", choices=CLASS_OPTIONS)
        index_no = gr.Dropdown(label="Index No", choices=[f"{i:02}" for i in range(1, 46)])

        policy_text = gr.Markdown(user_acceptance_policy)
        policy_checkbox = gr.Checkbox(label="I have read and agree to the Things to Note When using the Oral Coach ⚡ϞϞ(๑⚈ ․̫ ⚈๑)∩ ⚡", value=False)

        submit_info_btn = gr.Button("Submit Info")
        info_output = gr.Text()

        with gr.Column(visible=False) as oral_coach_content:
            gr.Markdown("## Powered by Hugging Face")
            gr.Markdown(img_html)
            with gr.Row():
                with gr.Column(scale=1):
                    gr.Markdown("### Step 1: Choose a Question")
                    question_choice = gr.Radio(thinkingframes.questions, label="Questions", value=thinkingframes.questions[0])
                    gr.Markdown("### Step 2: Choose a Thinking Frame")
                    strategy_choice = gr.Dropdown(list(strategy_options.keys()), label="Thinking Frame", value=list(strategy_options.keys())[0])
                    gr.Markdown("### Step 3: Choose Feedback Level")
                    feedback_level = gr.Radio(["Brief Feedback", "Moderate Feedback", "Comprehensive Feedback"], label="Feedback Level")
                    feedback_level.value = "Brief Feedback"

                with gr.Column(scale=1):
                    gr.Markdown("### Step 4: Record Your Answer")
                    audio_input = gr.Audio(type="numpy", sources=["microphone"], label="Record")
                    submit_answer_btn = gr.Button("Submit Oral Response")

                    gr.Markdown("### Step 5: Review your personalised feedback")
                    feedback_output = gr.Chatbot(label="Feedback", scale=4, height=700, show_label=True)
                    audio_output = gr.Audio(type="numpy", label="Audio Playback", format="wav", autoplay="True")

                    submit_answer_btn.click(
                        predict,
                        inputs=[question_choice, strategy_choice, feedback_level, audio_input],
                        outputs=[feedback_output, audio_output]
                    )

        submit_info_btn.click(
            toggle_oral_coach_visibility,
            inputs=[class_name, index_no, policy_checkbox],
            outputs=[info_output, oral_coach_content]
        )

    create_teachers_dashboard_tab()

demo.queue(max_size=20)
demo.launch()