simonraj commited on
Commit
64d72ac
·
1 Parent(s): 95d70c8

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -99
app.py DELETED
@@ -1,99 +0,0 @@
1
- import os
2
- import openai
3
- import gradio as gr
4
- import requests
5
- import speech_recognition as sr
6
- import io
7
- import base64
8
- import tempfile
9
-
10
- openai.api_key = "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"
11
-
12
- def transcribe_audio(audio_file):
13
- recognizer = sr.Recognizer()
14
- with sr.AudioFile(audio_file) as source:
15
- audio_data = recognizer.record(source)
16
- try:
17
- text = recognizer.recognize_google(audio_data)
18
- return text
19
- except sr.UnknownValueError:
20
- return "Could not understand audio"
21
- except sr.RequestError as e:
22
- return f"Could not request results: {e}"
23
-
24
- questions = [
25
- "What is your favourite food? How do you prepare it?",
26
- "What are three dishes that represent Singapore's diverse culture? Can you describe each dish and its cultural significance?",
27
- "What is your favourite meal of the day? Why do you prefer it over other meals?",
28
- "Do you prefer eating at hawker centres or restaurants, and why?"
29
- ]
30
-
31
- user_input_counter = 0
32
- current_question_index = 0
33
-
34
- def intelligent_tutor(audio_file, feedback=False):
35
- global user_input_counter
36
- global current_question_index
37
-
38
- input_text = transcribe_audio(audio_file)
39
- user_input_counter += 1
40
-
41
- conversation = [
42
- {"role": "system", "content": "Act as an oral coach for Primary 6 students in Singapore. In preparation for their PSLE Oral Examination, guide them in developing critical thinking skills by asking thought-provoking questions using the Socratic method. Always customize your approach to the individual student, breaking down complex problems into simpler components based on their knowledge and interests. The main theme and topic of the question is your favourite food."},
43
- {"role": "user", "content": input_text}
44
- ]
45
-
46
- if feedback:
47
- conversation.append({"role": "system", "content": "Summarize the conversation so far and provide areas where the user did well in the oral conversation and areas of improvement."})
48
-
49
- response = openai.ChatCompletion.create(
50
- model="gpt-3.5-turbo",
51
- messages=conversation,
52
- max_tokens=200
53
- )
54
-
55
- text_response = response.choices[0]['message']['content'].strip()
56
-
57
- if user_input_counter % 2 == 0 and not feedback:
58
- next_question = questions[current_question_index]
59
- current_question_index = (current_question_index + 1) % len(questions)
60
- text_response += f"\n\nNext question: {next_question}"
61
-
62
- # Request specific improvement suggestions
63
- conversation.append({"role": "system", "content": "Provide specific alternative statements the student can say to improve their responses."})
64
- response = openai.ChatCompletion.create(
65
- model="gpt-3.5-turbo",
66
- messages=conversation,
67
- max_tokens=200
68
- )
69
-
70
- improvement_response = response.choices[0]['message']['content'].strip()
71
- text_response += f"\n\nImprovement suggestions: {improvement_response}"
72
-
73
- return text_response[:1000] # Return only the first 1000 characters
74
-
75
-
76
-
77
-
78
- questions = """
79
- - What is your favourite food? How do you prepare it?
80
- - What are three dishes that represent Singapore's diverse culture? Can you describe each dish and its cultural significance?
81
- - What is your favourite meal of the day? Why do you prefer it over other meals?
82
- - Do you prefer eating at hawker centres or restaurants, and why?
83
- """
84
-
85
- iface = gr.Interface(
86
- fn=intelligent_tutor,
87
- inputs=[
88
- gr.Audio(source="microphone", type="filepath", label="Record audio"),
89
- gr.inputs.Checkbox(label="Request feedback"),
90
- ],
91
- outputs=gr.outputs.Textbox(label="Output Text"),
92
- title="Oral Coach for Primary 6 Students",
93
- description=(
94
- "An oral coach for Primary 6 students in Singapore that helps them prepare for their PSLE Oral Examination by guiding them in developing critical thinking skills using the Socratic method. "
95
- "Example questions:\n\n" + questions
96
- ),
97
- )
98
-
99
- iface.launch(share=True)