Commit
·
9b3da8d
1
Parent(s):
bf61626
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
import openai
|
4 |
+
import pandas as pd
|
5 |
+
|
6 |
+
# The OpenAI API key provided by the instructor
|
7 |
+
openai.api_key = os.environ.get("OPENAI_API_KEY")
|
8 |
+
# Instructor's prompt (invisible to students)
|
9 |
+
instructor_prompt = os.environ.get("SECRET_PROMPT")
|
10 |
+
|
11 |
+
def add_user_message(msg, history, messages):
|
12 |
+
messages.append({"role": "user", "content": msg})
|
13 |
+
return "", history + [[msg, None]], messages
|
14 |
+
|
15 |
+
def get_tutor_reply(history, messages):
|
16 |
+
# Generate a response from the chatbot
|
17 |
+
completion = openai.chat.completions.create(
|
18 |
+
model="gpt-3.5-turbo",
|
19 |
+
messages=messages,
|
20 |
+
max_tokens=150
|
21 |
+
)
|
22 |
+
# Extract the chatbot's reply
|
23 |
+
reply = completion.choices[0].message.content
|
24 |
+
history[-1][1] = reply
|
25 |
+
messages.append({"role": "assistant", "content": reply})
|
26 |
+
return history, messages
|
27 |
+
|
28 |
+
def save_chat_to_json(history):
|
29 |
+
formatted_convo = pd.DataFrame(history, columns=['user', 'chatbot'])
|
30 |
+
output_fname = f'tutoring_conversation.json'
|
31 |
+
formatted_convo.to_json(output_fname, orient='records')
|
32 |
+
return gr.update(value=output_fname, visible=True)
|
33 |
+
|
34 |
+
# Create a Gradio interface
|
35 |
+
with gr.Blocks() as SimpleChat:
|
36 |
+
gr.Markdown("""
|
37 |
+
## Chat with A Tutor
|
38 |
+
Description here
|
39 |
+
""")
|
40 |
+
messages = gr.JSON(
|
41 |
+
visible=False,
|
42 |
+
value = [{"role": "system", "content": instructor_prompt}]
|
43 |
+
)
|
44 |
+
with gr.Group():
|
45 |
+
chatbot = gr.Chatbot(label="Simple Tutor")
|
46 |
+
with gr.Row(equal_height=True):
|
47 |
+
user_chat_input = gr.Textbox(show_label=False, scale=9)
|
48 |
+
submit_btn = gr.Button("Enter", scale=1)
|
49 |
+
with gr.Group():
|
50 |
+
export_dialogue_button_json = gr.Button("Export your chat history as a .json file")
|
51 |
+
file_download = gr.Files(label="Download here", file_types=['.json'], visible=False)
|
52 |
+
|
53 |
+
submit_btn.click(
|
54 |
+
fn=add_user_message, inputs=[user_chat_input, chatbot, messages], outputs=[user_chat_input, chatbot, messages], queue=False
|
55 |
+
).then(
|
56 |
+
fn=get_tutor_reply, inputs=[chatbot, messages], outputs=[chatbot, messages], queue=True
|
57 |
+
)
|
58 |
+
user_chat_input.submit(
|
59 |
+
fn=add_user_message, inputs=[user_chat_input, chatbot, messages], outputs=[user_chat_input, chatbot, messages], queue=False
|
60 |
+
).then(
|
61 |
+
fn=get_tutor_reply, inputs=[chatbot, messages], outputs=[chatbot, messages], queue=True
|
62 |
+
)
|
63 |
+
export_dialogue_button_json.click(save_chat_to_json, chatbot, file_download, show_progress=True)
|
64 |
+
|
65 |
+
# Launch the Gradio app
|
66 |
+
if __name__ == "__main__":
|
67 |
+
SimpleChat.launch()
|