samekbotfactorycz commited on
Commit
723f362
·
1 Parent(s): f619280

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +121 -0
app.py ADDED
@@ -0,0 +1,121 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """ https://www.linkedin.com/pulse/building-chatbot-using-chatgpt-api-python-gradio-soo-wei-kang/
2
+ pip install gradio==3.21.0
3
+ pip install openai
4
+ """
5
+
6
+ import gradio as gr
7
+ import openai
8
+ import random
9
+ import time
10
+
11
+ # Set up OpenAI API key
12
+ openai.api_key = "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"
13
+
14
+
15
+ system_prompt = """
16
+ You are an upbeat, encouraging tutor who helps students understand concepts by explaining
17
+ ideas and asking students questions. Start by introducing yourself to the student as their AI-Tutor
18
+ who is happy to help them with any questions. Only ask one question at a time. First, ask them
19
+ what they would like to learn about. Wait for the response. Then ask them about their learning
20
+ level: Are you a high school student, a college student or a professional? Wait for their response.
21
+ Then ask them what they know already about the topic they have chosen. Wait for a response.
22
+ Given this information, help students understand the topic by providing explanations, examples,
23
+ analogies. These should be tailored to students learning level and prior knowledge or what they
24
+ already know about the topic.Give students explanations, examples, and analogies about the concept to help them understand.
25
+ You should guide students in an open-ended way. Do not provide immediate answers or
26
+ solutions to problems but help students generate their own answers by asking leading questions.
27
+ Ask students to explain their thinking. If the student is struggling or gets the answer wrong, try
28
+ asking them to do part of the task or remind the student of their goal and give them a hint. If
29
+ students improve, then praise them and show excitement. If the student struggles, then be
30
+ encouraging and give them some ideas to think about. When pushing students for information,
31
+ try to end your responses with a question so that students have to keep generating ideas. Once a
32
+ student shows an appropriate level of understanding given their learning level, ask them to
33
+ explain the concept in their own words; this is the best way to show you know something, or ask
34
+ them for examples. When a student demonstrates that they know the concept you can move the
35
+ conversation to a close and tell them you’re here to help if they have further questions.
36
+
37
+ Reply in Czech language
38
+ """
39
+
40
+ system_message = {"role": "system", "content": system_prompt}
41
+
42
+
43
+ header = "<h1>Jsem AI Tutor - budu vas ucit cemu chcete</h1>"
44
+
45
+ import gradio as gr
46
+ import random
47
+ import time
48
+
49
+
50
+ with gr.Blocks() as demo:
51
+ html = gr.HTML(header+system_prompt)
52
+ chatbot = gr.Chatbot()
53
+ msg = gr.Textbox()
54
+ clear = gr.Button("Clear")
55
+
56
+ state = gr.State([])
57
+
58
+ def user(user_message, history):
59
+ return "", history + [[user_message, None]]
60
+
61
+ def bot(history, messages_history):
62
+ user_message = history[-1][0]
63
+ bot_message, messages_history = ask_gpt(user_message, messages_history)
64
+ messages_history += [{"role": "assistant", "content": bot_message}]
65
+ history[-1][1] = bot_message
66
+ time.sleep(1)
67
+ return history, messages_history
68
+
69
+ def ask_gpt(message, messages_history):
70
+ messages_history += [
71
+ {"role": "user", "content": message},
72
+ {"role": "system", "content": system_prompt}
73
+ ]
74
+ response = openai.ChatCompletion.create(
75
+ model="gpt-3.5-turbo",
76
+ messages=messages_history
77
+ )
78
+ return response['choices'][0]['message']['content'], messages_history
79
+
80
+ def init_history(messages_history):
81
+ messages_history = []
82
+ messages_history += [system_message]
83
+ return messages_history
84
+
85
+ msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
86
+ bot, [chatbot, state], [chatbot, state]
87
+ )
88
+ #i = list()
89
+ #i[0][0] = ""
90
+ #arr = [[0]*1]*1
91
+ #arr[-1][0]
92
+ #bot( arr, arr)
93
+
94
+ clear.click(lambda: None, None, chatbot, queue=False).success(init_history, [state], [state])
95
+
96
+
97
+
98
+ demo.launch()
99
+
100
+
101
+ """with gr.Blocks() as demo:
102
+ chatbot = gr.Chatbot()
103
+ msg = gr.Textbox()
104
+ clear = gr.Button("Clear")
105
+
106
+ def user(user_message, history):
107
+ return "", history + [[user_message, None]]
108
+
109
+ def bot(history):
110
+ bot_message = random.choice(["Yes", "No"])
111
+ history[-1][1] = bot_message
112
+ time.sleep(1)
113
+ return history
114
+
115
+ msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
116
+ bot, chatbot, chatbot
117
+ )
118
+ clear.click(lambda: None, None, chatbot, queue=False)
119
+
120
+ demo.launch()
121
+ """