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"" ) 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', '
') 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', '
') wrapped_output_text = f'
{text_response}
' 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() + "
" + questions[0] + "
You have two attempts for each question.
" + "Please answer the displayed question at the output screen after the 1st Question."), ) iface.launch(share=False)