File size: 4,933 Bytes
8d66a4e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75839b1
 
8d66a4e
 
 
 
75839b1
8d66a4e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import openai
import gradio as gr
import base64
from data4 import strategy_text, description, questions

OPENAI_API_KEY = os.getenv("OPENAI_API_KEY") 
openai.api_key = OPENAI_API_KEY

def transcribe_audio(audio_file_path):
    # Use OpenAI's Whisper to transcribe the audio
    audio_file = open(audio_file_path, "rb")
    transcript = openai.Audio.transcribe("whisper-1", audio_file)
    return transcript["text"]

def get_base64_image():
    with open("SBC4.jpg", "rb") as img_file:
        return base64.b64encode(img_file.read()).decode("utf-8")
    
def get_image_html():
    return (
        f"<img src='data:image/jpeg;base64,{get_base64_image()}' style='display: block; margin-left: auto; margin-right: auto; padding-bottom: 15px; width: 300px;'>"
    )

current_question_index = 0
user_input_counter = 0
conversation_history = []

def intelligent_tutor(audio_file, provide_hints=False):
    global current_question_index
    global questions
    global user_input_counter
    global conversation_history

    input_text = transcribe_audio(audio_file)
    current_question = questions[current_question_index]

    if provide_hints:
        hint_message = f"[Tamil translation of: Consider using the {strategy_text[current_question_index]} to answer the question: '{questions[current_question_index]}']."  # Translate to Tamil
        return f"[Tamil translation of: Respond to this Question: {questions[current_question_index]}]", hint_message

    conversation = [
        {
            "role": "system",
            "content": f"[Tamil translation of: You are an expert Tamil Language Teacher guiding a student. The student is answering the question: '{questions[current_question_index]}'. Based on their response, provide direct feedback to help them improve their spoken skills. Emphasize areas of strength, suggest areas of improvement, and guide them on how to better answer using the {strategy_text[current_question_index]} strategy. The feedback should be in second person, addressing the student directly.]"
        },
        {"role": "user", "content": input_text}
    ]

    # Append the user's response to the conversation history
    conversation_history.append(input_text)

    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=conversation,
        max_tokens=400
    )

    if not response.choices:
        return "No response from the model.", ""

    text_response = response.choices[0]['message']['content'].strip()
    text_response = text_response.replace('\n', '<br>')

    user_input_counter += 1

    if user_input_counter % 2 == 0:
        if current_question_index + 1 < len(questions):
            current_question_index += 1
            next_question = questions[current_question_index]
            text_response += f"\n\nNext question ({current_question_index + 1}): {next_question}"
        else:
            # All questions have been answered, provide a summary
            summary_prompt = {
                "role": "system",
                "content": f"Based on the entire conversation, provide a detailed feedback summary highlighting the overall performance, strengths, and areas of improvement. Reference the student's responses and evaluate how well they used the {strategy_text[current_question_index]} strategy to structure their answers. Format the feedback in bullet points."
            }
            summary_conversation = [summary_prompt, {"role": "user", "content": " ".join(conversation_history)}]
            
            summary_response = openai.ChatCompletion.create(
                model="gpt-3.5-turbo",
                messages=summary_conversation,
                max_tokens=600  # Increased token limit for detailed summary
            )
            
            if not summary_response.choices:
                return "No response from the model.", ""
            
            text_response = summary_response.choices[0]['message']['content'].strip()
            text_response = text_response.replace('\n', '<br>')

    wrapped_output_text = f'<div style="height: 300px; overflow-y: scroll;">{text_response}</div>'
    return f"Current Question: {questions[current_question_index]}", wrapped_output_text

iface = gr.Interface(
    fn=intelligent_tutor,
    inputs=[
        gr.Audio(source="microphone", type="filepath", label="Record audio", sampling_rate=16000),
        gr.inputs.Checkbox(label="Provide Summary of Conversation"),  # Checkbox for hints
    ],
    outputs=[
        gr.outputs.HTML(label="Question"),
        gr.outputs.HTML(label="Output Text"),
    ],
    title="Oral Coach for Stimulus Based Conversation",
    description=(get_image_html() + 
                 "<br> " + questions[0] +
                 "<br>You have two attempts for each question.<br>" +
                 "<b>Please answer the displayed question at the output screen after the 1st Question.</b>"),
)
iface.launch(share=False)