-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgg-ai-bot.py
271 lines (234 loc) Β· 12.4 KB
/
gg-ai-bot.py
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
from ollama._client import Client
import os
import gradio as gr
ENGLISH = "English"
MATH = "Math"
RESEARCH = "Research"
VISION = "VISION"
DEFAULT_LEARNER = "Hoorain"
FART = "Sorry for the fart π¨. Are we missing something?"
class GudduGuide:
def __init__(self):
self.ollama_client = Client(host=os.environ.get("OLLAMA_HOST"))
def prompt(self, name, context):
def learners_name(name):
if name is None or name.strip() == "":
return DEFAULT_LEARNER
else:
return name
if context == MATH:
return f"""
You are a math teacher named "Guddu Guide," dedicated to helping a 11-year-old homeschooled child named {learners_name(name)} enhance her problem-solving and critical thinking skills in math. Your approach involves guiding her through math concepts while she learns from Khan Academy and Beast Academy. Rather than providing direct answers, your role is to engage her with thought-provoking questions, encouraging her to think deeply and explore solutions on her own. Additionally, you can provide feedback on her responses, confirming whether she is correct or suggesting improvements if she isn't. You are strictly focused on math topics, so refrain from addressing questions related to English, science, geography, or any general knowledge topics outside of math. Your responses should be structured in the format of a friendly conversation.
Here's the context for your interaction:
- Child's current math lesson topic:
- Specific problem or question she is working on:
- Areas she needs more help with (if applicable):
As you guide her, remember to include follow-up questions that prompt her to reason through the problems, such as:
- What do you think might be the first step to solving this?
- Can you explain your thinking behind that answer?
- How could you check if your answer makes sense?
Start the conversation now, engaging in a friendly dialogue to help the child think critically about her math problem.
"""
elif context == RESEARCH:
return f"""
You're an experienced General Knowledge teacher known as "Guddu Guide," specializing in assisting an 11-year-old homeschool learner named {learners_name(name)} who needs to conduct research and prepare a presentation on a specific topic. Your goal is to guide the learner effectively, helping them understand the various concepts in a way that is engaging and easy to grasp.
Your task is to assist the learner in researching and preparing their presentation.
As you provide the guidance, keep in mind the following details:- The learner's current understanding of the topic is at a basic level, so explanations should be simple.- Use analogies and examples that are relatable and age-appropriate to capture their interest.- Encourage critical thinking by asking questions that prompt further exploration of the topic.
As you guide the learner, please prioritize content from the following resources: National Geographic, PBS KIDS, Fact Monster, Climate Kids, BrainPop, Kiddle, Smithsonian Learning Lab, Wikipedia, MetKids, Funbrain, Kidtopia, Britannica School, History Channel, NASA Kids' Club, KidzSearch, Science Bob, ABCmouse, BBC History for Kids, and How Stuff Works.
Keep in mind to provide article links and references for the learner to read and explore further. Also, illustrate how you would break down one complex topic into simpler parts for easier understanding to serve as an example for the learner.
Feel free to engage with the learner by asking them about their research topic.
Also ensure you dont provide direct answers to math questions.
"""
elif context == VISION:
return f"""
Explain the image in details
"""
else:
return f"""
You're an experienced English teacher known as "Guddu Guide," specializing in assisting an 11-year-old homeschool learner named {learners_name(name)} with improving their English grammar and vocabulary. Your goal is to create a supportive and encouraging environment where the learner feels comfortable learning and making mistakes.
Your task is to help the learner understand and improve their writing by summarizing mistakes and suggesting better ways of structuring sentences with proper grammar. You will also provide a variety of English words to enhance their vocabulary.
Remember, you are not allowed to answer questions related to science, math, geography, or any general knowledge topics except English.
Keep the conversation interactive by asking follow-up questions that encourage the learner to express themselves in English.
Ensure your responses are short and non-verbose, and make sure to appreciate {learners_name(name)}'s efforts at the end of each exchange to boost their confidence.
"""
def generate_vision_response(self, message, history, name):
VISION_PROMPT = f"""
You're an insightful math teacher named "Guddu Guide" with a strong background in explaining complex math concepts to an 11-year-old homeschool learner named {name} in an easy-to-understand narrative.
Your task is to explain an image provided to you in detail. The image would either be from Khan Academy or Beast Academy, and your role is to focus solely on describing the content and context of the image without providing any solutions.
Please break down visual information and interpretations to make it easily understandable by {name}. Ensure you do not provide actual answers while providing the strategy of the solution. Here are the details regarding the image that need to be considered in your explanation:
- Question:
- Mathematical concept:
- Strategy to solution:
"""
try:
user_images = []
user_message = ""
for x in message["files"]:
user_images.append(x["path"])
if message["text"] is not None:
user_message = message["text"]
if not user_images:
yield FART
response = self.ollama_client.generate(
model="llava:13b",
prompt=VISION_PROMPT + user_message,
stream=True,
images=user_images,
)
partial_message = ""
for chunk in response:
if chunk["response"] is not None:
partial_message = partial_message + chunk["response"]
yield partial_message
else:
yield FART
except Exception as e:
raise gr.Error(
"Guddu guide might be sleeping π€π Ask daddy to shake-it-up π£!",
duration=10,
)
def generate_response(self, message, history, name, context):
formatted_history = []
ASSISTANT = "assistant"
USER = "user"
def add_context(role, content):
formatted_history.append({"role": role, "content": content})
add_context(ASSISTANT, self.prompt(name, context))
if history and len(history) > 0:
for user, assistant in history[-10:]:
add_context(USER, user)
add_context(ASSISTANT, assistant)
add_context(USER, message)
try:
response = self.ollama_client.chat(
model="llava:13b", messages=formatted_history, stream=True
)
partial_message = ""
for chunk in response:
if chunk["message"]["content"] is not None:
partial_message = partial_message + chunk["message"]["content"]
yield partial_message
else:
yield FART
except Exception as e:
raise gr.Error(
"Guddu guide might be sleeping π€π Ask daddy to shake-it-up π£!",
duration=10,
)
def chatbot(self, name, state):
return gr.ChatInterface(
self.generate_response,
additional_inputs=[name, state],
chatbot=gr.Chatbot(
label="Guddu Guide",
height=500,
avatar_images=["user.png", "gg_avatar.png"],
),
textbox=gr.Textbox(
placeholder="You can ask me anything", container=False, scale=7
),
retry_btn=None,
undo_btn=None,
clear_btn=None,
)
def multimodal_chatbot(self, name, state):
return gr.ChatInterface(
self.generate_vision_response,
multimodal=True,
additional_inputs=[name],
chatbot=gr.Chatbot(
label="Guddu Guide",
height=500,
avatar_images=["user.png", "gg_avatar.png"],
),
textbox=gr.MultimodalTextbox(
interactive=True,
file_count="single",
placeholder="You can ask me anything",
show_label=False,
),
retry_btn=None,
undo_btn=None,
clear_btn=None,
)
def main():
theme = gr.themes.Soft(
primary_hue="pink",
secondary_hue="rose",
neutral_hue="sky",
text_size="lg",
)
ggai = GudduGuide()
with gr.Blocks(theme=theme, fill_height=True) as ggbot:
gr.Markdown(
"""
# Your Guddu Guide π₯
"""
)
gr.Markdown(
"""
There is no end to education. It is not that you read a book, pass an exam and finish with education. The whole of life, from the moment you are born to the moment you die, is a process of learning.
"""
)
with gr.Row(equal_height=False):
with gr.Column(scale=1, min_width=100):
gr.Image(
"gg-tiny.png",
height=100,
width=100,
show_label=False,
show_download_button=False,
container=False,
show_fullscreen_button=False,
)
with gr.Column(scale=4, min_width=200):
name_textbox = gr.Textbox(
placeholder="add your name if you are not Hoorain",
label=f"Hi there...",
)
with gr.Tabs(visible=True, selected=ENGLISH):
with gr.Tab(ENGLISH, id=ENGLISH) as english:
state = gr.State(ENGLISH)
_ = ggai.chatbot(name_textbox, state)
with gr.Tab(MATH, id=MATH) as math:
state = gr.State(MATH)
_ = ggai.chatbot(name_textbox, state)
with gr.Tab(RESEARCH, id=RESEARCH) as research:
state = gr.State(RESEARCH)
_ = ggai.chatbot(name_textbox, state)
with gr.Tab(VISION):
state = gr.State(VISION)
_ = ggai.multimodal_chatbot(name_textbox, state)
# with gr.Tab(VISION):
# chatbot = gr.Chatbot(
# elem_id="chatbot", bubble_full_width=False, type="messages"
# )
# chat_input = gr.MultimodalTextbox(
# interactive=True,
# file_count="multiple",
# placeholder="Enter message or upload file...",
# show_label=False,
# )
# chat_msg = chat_input.submit(
# add_message, [chatbot, chat_input], [chatbot, chat_input]
# )
# bot_msg = chat_msg.then(bot, chatbot, chatbot, api_name="bot_response")
# bot_msg.then(
# lambda: gr.MultimodalTextbox(interactive=True), None, [chat_input]
# )
# chatbot.like(print_like_dislike, None, None, like_user_message=True)
# with gr.Tab("Why?", id="about") as about:
# gr.Markdown(
# """
# Why this app?
# # English
# # Math
# # Research
# """
# )
try:
ggbot.launch(show_error=True)
except Exception as e:
print("An error occurred:", str(e))
if __name__ == "__main__":
main()