Commit
·
dd1df32
1
Parent(s):
2763962
Add app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from dotenv import load_dotenv
|
| 3 |
+
import openai
|
| 4 |
+
import random
|
| 5 |
+
import requests
|
| 6 |
+
from ast import literal_eval
|
| 7 |
+
import json
|
| 8 |
+
from enum import Enum
|
| 9 |
+
import gradio as gr
|
| 10 |
+
|
| 11 |
+
load_dotenv()
|
| 12 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 13 |
+
CHAT_ENDPOINT="https://api.openai.com/v1/chat/completions"
|
| 14 |
+
CHAT_MODEL = "gpt-3.5-turbo"
|
| 15 |
+
CHAT_AUTH = {"Authorization": "Bearer " + openai.api_key}
|
| 16 |
+
gpt_history = []
|
| 17 |
+
|
| 18 |
+
class ChatRoles():
|
| 19 |
+
SYSTEM = "system"
|
| 20 |
+
ASSISTANT = "assistant"
|
| 21 |
+
USER = "user"
|
| 22 |
+
|
| 23 |
+
def get_assistant_response():
|
| 24 |
+
params = {
|
| 25 |
+
"model": CHAT_MODEL,
|
| 26 |
+
"messages": gpt_history,
|
| 27 |
+
}
|
| 28 |
+
response = requests.post(url=CHAT_ENDPOINT, json=params, headers=CHAT_AUTH)
|
| 29 |
+
response_message = literal_eval(response.content.decode("utf-8"))["choices"][0]["message"]["content"]
|
| 30 |
+
gpt_history.append({"role": ChatRoles.ASSISTANT, "content": response_message})
|
| 31 |
+
print("\n" + response_message)
|
| 32 |
+
return response_message
|
| 33 |
+
|
| 34 |
+
instructions = {
|
| 35 |
+
1: '''Tell the user you are an AI-powered college counselor from Cledge helping students brainstorm ideas for their personal statement.
|
| 36 |
+
Generate 5 questions to help them brainstorm.''',
|
| 37 |
+
5: "Based on these responses, ask follow up questions that help discover common themes between the experiences.",
|
| 38 |
+
9: "Based on these responses, think of 5 ideas for personal statement essays. Write a synopsis of each idea."
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
def grad_demo():
|
| 42 |
+
with gr.Blocks() as demo:
|
| 43 |
+
def user(user_message, history):
|
| 44 |
+
gpt_history.append({"role": ChatRoles.USER, "content": user_message})
|
| 45 |
+
return "", history + [[user_message, None]]
|
| 46 |
+
def bot(history):
|
| 47 |
+
step = len(history)
|
| 48 |
+
print(f"STEP: {step}")
|
| 49 |
+
if step in instructions:
|
| 50 |
+
gpt_history.append({"role": ChatRoles.SYSTEM, "content": instructions[step]})
|
| 51 |
+
bot_message = get_assistant_response()
|
| 52 |
+
history[-1][1] = bot_message
|
| 53 |
+
gpt_history.append({"role": ChatRoles.ASSISTANT, "content": bot_message})
|
| 54 |
+
return history
|
| 55 |
+
chatbot = gr.Chatbot(value = lambda : bot([[None, None]]))
|
| 56 |
+
msg = gr.Textbox()
|
| 57 |
+
clear = gr.Button("Clear")
|
| 58 |
+
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
| 59 |
+
bot, chatbot, chatbot
|
| 60 |
+
)
|
| 61 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
| 62 |
+
demo.launch()
|
| 63 |
+
|
| 64 |
+
if __name__ == "__main__":
|
| 65 |
+
grad_demo()
|
| 66 |
+
#main()
|