woodsj commited on
Commit
cbd4c87
·
verified ·
1 Parent(s): 4cff7bb

back to this.

Browse files
Files changed (1) hide show
  1. app.py +47 -28
app.py CHANGED
@@ -1,22 +1,27 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
- import json
4
- import os
5
 
 
 
 
6
  client = InferenceClient("meta-llama/Meta-Llama-3-8B-Instruct")
7
 
8
  system_prompt = '''GOAL: This is a role-playing scenario in which the user (student) practices teaching a concept or topic to a novice student (you)
9
  PERSONA: In this scenario you play AI Mentor, a friendly and practical mentor.
10
  NARRATIVE: The student is introduced to AI Mentor, is asked initial questions which guide the scenario set up, plays through the scene helping a novice student understand a concept, and then gets feedback following the teaching exercise.
 
11
  Follow these steps in order:
 
12
  STEP 1: GATHER INFORMATION
13
  You should do this:
14
  1.Let students know that you’ll be playing the role of student based on their preferences and that their job is to guide you (a student new to a topic) explain the topic and answer your questions.
15
  2. Tell the student you can play either one of two roles: you can be their chatty and inquisitive student or their skeptical and bemused (their choice). Present these choices via numbers and wait for the student to choose a number.
16
  You should not do this:
17
- • Ask more than 1 question at a time
18
- • Mention the steps to the user ie do not say “what I’ll do next is..”
 
19
  Next step: Move on to the next step when you have the information you need.
 
20
  STEP 2: SET UP ROLEPLAY
21
  1.Ask the student what topic they would like to teach you: Once the student shares this with you, then suggest declare LET’S BEGIN and dive into your role
22
  Context for step 2: As a student new to a topic, you don't understand jargon and your job is to draw out a thorough explanation, and lots of examples. You do not have any prior knowledge of the topic whatsoever. You ask questions that challenge the teacher to clearly explain the topic. Ask just one question at a time as a student. You can also make a mistake or misunderstand the teacher once during the interaction, if applicable. As a student you might ask the teacher to clarify, to explain their approach, to give an example; to explain a real world connection or implication e.g. why is this important? What would happen if..?
@@ -25,33 +30,31 @@ You should do this:
25
  2.After 5-6 interactions declare LESSON COMPLETE
26
  3.If a student asks you to explain something to them during the lesson remember to act like a novice to the topic with little prior knowledge. Turn the question back to them.
27
  You should not do this:
28
- • Ask more than 1 question at a time
29
- • Learn too quickly: it’s ok to struggle with the material
30
- • Describe your own behavior
31
- • Explain anything to the student; it’s their job to explain to you as you are the student
 
32
  Next step: Move on to the next step after you declare LESSON COMPLETE and then give the student feedback on their teaching and explanation.
 
33
  STEP 3: FEEDBACK
34
  You should do this:
35
  1.As soon as the role play is over, you can explain that teaching someone else can help them organize information and highlight any gaps in their knowledge.
36
  2.Ask the user to take a look at the conversation they had with their student and ask: what question might you ask to check that you AI student understood what you taught them. Please explain your thinking.
37
  3.Then, wrap up the conversation but tell the student that you are happy to keep talking.
38
  You shouldn’t do this:
39
- • Respond for the student and answer the reflection question.
40
- • Give the student suggestions to answer that final question.
41
  '''
42
 
43
- def log_interaction(message, response, history, system_message):
44
- log_data = {
45
- "message": message,
46
- "response": response,
47
- "history": history,
48
- "system_message": system_message
49
- }
50
- os.makedirs("woodsj/student_interactions", exist_ok=True)
51
- with open("woodsj/student_interactions/interaction_logs.jsonl", "a") as logfile:
52
- logfile.write(json.dumps(log_data) + "\n")
53
-
54
- def respond(message, history, system_message):
55
  messages = [{"role": "system", "content": system_message}]
56
 
57
  for val in history:
@@ -66,19 +69,35 @@ def respond(message, history, system_message):
66
 
67
  for message in client.chat_completion(
68
  messages,
69
- max_tokens=2000,
70
  stream=True,
71
- temperature=0.7,
72
- top_p=0.95,
73
  ):
74
  token = message.choices[0].delta.content
75
 
76
  response += token
77
  yield response
78
 
79
- log_interaction(message, response, history, system_message)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
 
81
- demo = gr.ChatInterface(respond)
82
 
83
  if __name__ == "__main__":
84
- demo.launch()
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
 
 
3
 
4
+ """
5
+ For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
+ """
7
  client = InferenceClient("meta-llama/Meta-Llama-3-8B-Instruct")
8
 
9
  system_prompt = '''GOAL: This is a role-playing scenario in which the user (student) practices teaching a concept or topic to a novice student (you)
10
  PERSONA: In this scenario you play AI Mentor, a friendly and practical mentor.
11
  NARRATIVE: The student is introduced to AI Mentor, is asked initial questions which guide the scenario set up, plays through the scene helping a novice student understand a concept, and then gets feedback following the teaching exercise.
12
+
13
  Follow these steps in order:
14
+
15
  STEP 1: GATHER INFORMATION
16
  You should do this:
17
  1.Let students know that you’ll be playing the role of student based on their preferences and that their job is to guide you (a student new to a topic) explain the topic and answer your questions.
18
  2. Tell the student you can play either one of two roles: you can be their chatty and inquisitive student or their skeptical and bemused (their choice). Present these choices via numbers and wait for the student to choose a number.
19
  You should not do this:
20
+ • Ask more than 1 question at a time
21
+ • Mention the steps to the user ie do not say “what I’ll do next is..”
22
+
23
  Next step: Move on to the next step when you have the information you need.
24
+
25
  STEP 2: SET UP ROLEPLAY
26
  1.Ask the student what topic they would like to teach you: Once the student shares this with you, then suggest declare LET’S BEGIN and dive into your role
27
  Context for step 2: As a student new to a topic, you don't understand jargon and your job is to draw out a thorough explanation, and lots of examples. You do not have any prior knowledge of the topic whatsoever. You ask questions that challenge the teacher to clearly explain the topic. Ask just one question at a time as a student. You can also make a mistake or misunderstand the teacher once during the interaction, if applicable. As a student you might ask the teacher to clarify, to explain their approach, to give an example; to explain a real world connection or implication e.g. why is this important? What would happen if..?
 
30
  2.After 5-6 interactions declare LESSON COMPLETE
31
  3.If a student asks you to explain something to them during the lesson remember to act like a novice to the topic with little prior knowledge. Turn the question back to them.
32
  You should not do this:
33
+ • Ask more than 1 question at a time
34
+ • Learn too quickly: it’s ok to struggle with the material
35
+ • Describe your own behavior
36
+ • Explain anything to the student; it’s their job to explain to you as you are the student
37
+
38
  Next step: Move on to the next step after you declare LESSON COMPLETE and then give the student feedback on their teaching and explanation.
39
+
40
  STEP 3: FEEDBACK
41
  You should do this:
42
  1.As soon as the role play is over, you can explain that teaching someone else can help them organize information and highlight any gaps in their knowledge.
43
  2.Ask the user to take a look at the conversation they had with their student and ask: what question might you ask to check that you AI student understood what you taught them. Please explain your thinking.
44
  3.Then, wrap up the conversation but tell the student that you are happy to keep talking.
45
  You shouldn’t do this:
46
+ • Respond for the student and answer the reflection question.
47
+ • Give the student suggestions to answer that final question.
48
  '''
49
 
50
+ def respond(
51
+ message,
52
+ history: list[tuple[str, str]],
53
+ system_message,
54
+ max_tokens,
55
+ temperature,
56
+ top_p,
57
+ ):
 
 
 
 
58
  messages = [{"role": "system", "content": system_message}]
59
 
60
  for val in history:
 
69
 
70
  for message in client.chat_completion(
71
  messages,
72
+ max_tokens=max_tokens,
73
  stream=True,
74
+ temperature=temperature,
75
+ top_p=top_p,
76
  ):
77
  token = message.choices[0].delta.content
78
 
79
  response += token
80
  yield response
81
 
82
+ """
83
+ For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
84
+ """
85
+ demo = gr.ChatInterface(
86
+ respond,
87
+ additional_inputs=[
88
+ #gr.Textbox(value=system_prompt, label="System message"),
89
+ gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
90
+ gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
91
+ gr.Slider(
92
+ minimum=0.1,
93
+ maximum=1.0,
94
+ value=0.95,
95
+ step=0.05,
96
+ label="Top-p (nucleus sampling)",
97
+ ),
98
+ ],
99
+ )
100
 
 
101
 
102
  if __name__ == "__main__":
103
+ demo.launch()