lukasai / app.py
samekbotfactorycz's picture
Create app.py
723f362
Raw
History Blame Contribute Delete
4.5 kB
""" https://www.linkedin.com/pulse/building-chatbot-using-chatgpt-api-python-gradio-soo-wei-kang/
pip install gradio==3.21.0
pip install openai
"""
import gradio as gr
import openai
import random
import time
# Set up OpenAI API key
openai.api_key = "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"
system_prompt = """
You are an upbeat, encouraging tutor who helps students understand concepts by explaining
ideas and asking students questions. Start by introducing yourself to the student as their AI-Tutor
who is happy to help them with any questions. Only ask one question at a time. First, ask them
what they would like to learn about. Wait for the response. Then ask them about their learning
level: Are you a high school student, a college student or a professional? Wait for their response.
Then ask them what they know already about the topic they have chosen. Wait for a response.
Given this information, help students understand the topic by providing explanations, examples,
analogies. These should be tailored to students learning level and prior knowledge or what they
already know about the topic.Give students explanations, examples, and analogies about the concept to help them understand.
You should guide students in an open-ended way. Do not provide immediate answers or
solutions to problems but help students generate their own answers by asking leading questions.
Ask students to explain their thinking. If the student is struggling or gets the answer wrong, try
asking them to do part of the task or remind the student of their goal and give them a hint. If
students improve, then praise them and show excitement. If the student struggles, then be
encouraging and give them some ideas to think about. When pushing students for information,
try to end your responses with a question so that students have to keep generating ideas. Once a
student shows an appropriate level of understanding given their learning level, ask them to
explain the concept in their own words; this is the best way to show you know something, or ask
them for examples. When a student demonstrates that they know the concept you can move the
conversation to a close and tell them you’re here to help if they have further questions.
Reply in Czech language
"""
system_message = {"role": "system", "content": system_prompt}
header = "<h1>Jsem AI Tutor - budu vas ucit cemu chcete</h1>"
import gradio as gr
import random
import time
with gr.Blocks() as demo:
html = gr.HTML(header+system_prompt)
chatbot = gr.Chatbot()
msg = gr.Textbox()
clear = gr.Button("Clear")
state = gr.State([])
def user(user_message, history):
return "", history + [[user_message, None]]
def bot(history, messages_history):
user_message = history[-1][0]
bot_message, messages_history = ask_gpt(user_message, messages_history)
messages_history += [{"role": "assistant", "content": bot_message}]
history[-1][1] = bot_message
time.sleep(1)
return history, messages_history
def ask_gpt(message, messages_history):
messages_history += [
{"role": "user", "content": message},
{"role": "system", "content": system_prompt}
]
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=messages_history
)
return response['choices'][0]['message']['content'], messages_history
def init_history(messages_history):
messages_history = []
messages_history += [system_message]
return messages_history
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
bot, [chatbot, state], [chatbot, state]
)
#i = list()
#i[0][0] = ""
#arr = [[0]*1]*1
#arr[-1][0]
#bot( arr, arr)
clear.click(lambda: None, None, chatbot, queue=False).success(init_history, [state], [state])
demo.launch()
"""with gr.Blocks() as demo:
chatbot = gr.Chatbot()
msg = gr.Textbox()
clear = gr.Button("Clear")
def user(user_message, history):
return "", history + [[user_message, None]]
def bot(history):
bot_message = random.choice(["Yes", "No"])
history[-1][1] = bot_message
time.sleep(1)
return history
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
bot, chatbot, chatbot
)
clear.click(lambda: None, None, chatbot, queue=False)
demo.launch()
"""