|
import gradio as gr |
|
import openai |
|
import os |
|
import data6 |
|
import base64 |
|
|
|
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY") |
|
openai.api_key = OPENAI_API_KEY |
|
|
|
def image_to_base64(img_path): |
|
with open(img_path, "rb") as img_file: |
|
return base64.b64encode(img_file.read()).decode('utf-8') |
|
|
|
img_base64 = image_to_base64("SBC6.jpg") |
|
img_html = f'<img src="data:image/jpg;base64,{img_base64}" alt="SBC6" width="300" style="display: block; margin: auto;"/>' |
|
|
|
def predict(question_choice, audio): |
|
|
|
with open(audio, "rb") as audio_file: |
|
transcript = openai.Audio.transcribe("whisper-1", audio_file) |
|
message = transcript["text"] |
|
|
|
|
|
current_question_index = data6.questions.index(question_choice) |
|
strategy, explanation = data6.strategy_text[current_question_index] |
|
|
|
|
|
conversation = [ |
|
{ |
|
"role": "system", |
|
"content": f"You are an expert English Language Teacher in a Singapore Primary school, directly guiding a Primary 6 student in Singapore. The student is answering the question: '{data6.questions[current_question_index]}'. Point out areas they did well and where they can improve. Then, provide a suggested answer using the {data6.strategy_text[current_question_index][0]} strategy. Encourage the use of sophisticated vocabulary and expressions. For the second and third questions, the picture is not relevant, so the student should not refer to it in their response. {explanation} The feedback should be in second person, addressing the student directly." |
|
}, |
|
{"role": "user", "content": message} |
|
] |
|
|
|
|
|
response = openai.ChatCompletion.create( |
|
model='gpt-3.5-turbo', |
|
messages=conversation, |
|
temperature=0.4, |
|
max_tokens=400, |
|
stream=True |
|
) |
|
|
|
partial_message = "" |
|
for chunk in response: |
|
if len(chunk['choices'][0]['delta']) != 0: |
|
partial_message = partial_message + chunk['choices'][0]['delta']['content'] |
|
yield partial_message |
|
|
|
|
|
def get_image_html(): |
|
return "" |
|
|
|
|
|
|
|
iface = gr.Interface( |
|
fn=predict, |
|
inputs=[ |
|
gr.Radio(data6.questions, label="Choose a question", default=data6.questions[0]), |
|
gr.inputs.Audio(source="microphone", type="filepath") |
|
], |
|
outputs=gr.inputs.Textbox(), |
|
description=img_html, |
|
css="custom.css" |
|
) |
|
iface.queue().launch() |
|
|
|
|