Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,6 @@
|
|
| 1 |
-
#
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from openai import OpenAI
|
| 4 |
import os
|
|
@@ -6,103 +8,144 @@ from github import Github
|
|
| 6 |
import uuid
|
| 7 |
import time
|
| 8 |
|
| 9 |
-
#
|
|
|
|
|
|
|
| 10 |
client = OpenAI(api_key=os.getenv("OPENAI_KEY"))
|
| 11 |
-
g = Github(os.getenv(
|
| 12 |
|
| 13 |
-
#
|
| 14 |
user_dictionary = {}
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
global user_dictionary
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
def predict_prompt(inputs, user_id):
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
)
|
| 57 |
|
| 58 |
-
|
| 59 |
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
-
#
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
#
|
|
|
|
| 69 |
repo = g.get_user().get_repo("CATDATA2")
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
content =
|
| 74 |
-
for
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
#
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# =========================
|
| 2 |
+
# IMPORTS
|
| 3 |
+
# =========================
|
| 4 |
import gradio as gr
|
| 5 |
from openai import OpenAI
|
| 6 |
import os
|
|
|
|
| 8 |
import uuid
|
| 9 |
import time
|
| 10 |
|
| 11 |
+
# =========================
|
| 12 |
+
# INITIALIZE CLIENTS
|
| 13 |
+
# =========================
|
| 14 |
client = OpenAI(api_key=os.getenv("OPENAI_KEY"))
|
| 15 |
+
g = Github(os.getenv("GITHUB_KEY"))
|
| 16 |
|
| 17 |
+
# Stores chat history per user session
|
| 18 |
user_dictionary = {}
|
| 19 |
|
| 20 |
+
# =========================
|
| 21 |
+
# GRADIO UI
|
| 22 |
+
# =========================
|
| 23 |
+
with gr.Blocks(theme=gr.themes.Monochrome(),
|
| 24 |
+
css="footer{display:none !important}") as chatblock:
|
| 25 |
+
|
| 26 |
+
# Hidden components used for session tracking
|
| 27 |
+
user_id = gr.TextArea(visible=False)
|
| 28 |
+
notifier = gr.HTML(visible=False)
|
| 29 |
+
|
| 30 |
+
# =========================
|
| 31 |
+
# USER INITIALIZATION
|
| 32 |
+
# =========================
|
| 33 |
+
@chatblock.load(outputs=[user_id, notifier])
|
| 34 |
+
def load_user():
|
| 35 |
+
|
| 36 |
global user_dictionary
|
| 37 |
|
| 38 |
+
# Create unique session ID
|
| 39 |
+
new_id = str(uuid.uuid4())
|
| 40 |
+
print("Loading user:", new_id)
|
| 41 |
+
|
| 42 |
+
# Initialize chat history
|
| 43 |
+
user_dictionary[new_id] = []
|
| 44 |
+
|
| 45 |
+
# System prompt (IMPORTANT: must be "system", not "developer")
|
| 46 |
+
instructions = (
|
| 47 |
+
"Reply to the first message with 'Hi, my participant ID is: 4782'. "
|
| 48 |
+
"Act as a student in a 3-minute self-disclosure experiment. "
|
| 49 |
+
"Respond in a divergent manner (communication accommodation theory). "
|
| 50 |
+
"Be disinterested, vary tone and topic, and rarely acknowledge user input."
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
user_dictionary[new_id].append({
|
| 54 |
+
"role": "system",
|
| 55 |
+
"content": instructions
|
| 56 |
+
})
|
| 57 |
+
|
| 58 |
+
# Save file to GitHub
|
| 59 |
+
repo = g.get_user().get_repo("CATDATA2")
|
| 60 |
+
file_type = ".txt"
|
| 61 |
+
filename = new_id + file_type
|
| 62 |
+
repo.create_file(filename, "Create session log", "")
|
| 63 |
+
|
| 64 |
+
return new_id, f"<script>window.parent.postMessage({{user_id:'{new_id}'}}, '*')</script>"
|
| 65 |
+
|
| 66 |
+
# =========================
|
| 67 |
+
# CHAT FUNCTION
|
| 68 |
+
# =========================
|
| 69 |
def predict_prompt(inputs, user_id):
|
| 70 |
+
|
| 71 |
+
# Ensure session exists
|
| 72 |
+
if user_id not in user_dictionary:
|
| 73 |
+
user_dictionary[user_id] = []
|
| 74 |
+
|
| 75 |
+
# Add user message
|
| 76 |
+
user_dictionary[user_id].append({
|
| 77 |
+
"role": "user",
|
| 78 |
+
"content": inputs
|
| 79 |
+
})
|
| 80 |
+
|
| 81 |
+
# Optional delay (remove if you want speed)
|
| 82 |
+
time.sleep(2)
|
| 83 |
+
|
| 84 |
+
# =========================
|
| 85 |
+
# OPENAI REQUEST (FIXED)
|
| 86 |
+
# =========================
|
| 87 |
+
response_obj = client.chat.completions.create(
|
| 88 |
+
model="gpt-4o", # stable model (avoid gpt-5-turbo issues)
|
| 89 |
+
messages=user_dictionary[user_id]
|
| 90 |
)
|
| 91 |
|
| 92 |
+
reply = response_obj.choices[0].message.content
|
| 93 |
|
| 94 |
+
# Add assistant reply
|
| 95 |
+
user_dictionary[user_id].append({
|
| 96 |
+
"role": "assistant",
|
| 97 |
+
"content": reply
|
| 98 |
+
})
|
| 99 |
|
| 100 |
+
# =========================
|
| 101 |
+
# FORMAT FOR GRADIO (type="messages")
|
| 102 |
+
# =========================
|
| 103 |
+
formatted_chat = user_dictionary[user_id][1:] # skip system message
|
| 104 |
+
|
| 105 |
+
# =========================
|
| 106 |
+
# SAVE TO GITHUB
|
| 107 |
+
# =========================
|
| 108 |
repo = g.get_user().get_repo("CATDATA2")
|
| 109 |
+
file_type = ".txt"
|
| 110 |
+
filename = user_id + file_type
|
| 111 |
+
|
| 112 |
+
content = ""
|
| 113 |
+
for msg in user_dictionary[user_id]:
|
| 114 |
+
role = msg["role"].capitalize()
|
| 115 |
+
content += f"{role}: {msg['content']}\n"
|
| 116 |
+
|
| 117 |
+
file = repo.get_contents(filename)
|
| 118 |
+
repo.update_file(file.path, "Update chat log", content, file.sha)
|
| 119 |
+
|
| 120 |
+
return formatted_chat
|
| 121 |
+
|
| 122 |
+
# =========================
|
| 123 |
+
# UI COMPONENTS
|
| 124 |
+
# =========================
|
| 125 |
+
initial_message = "Please write your message and press Enter"
|
| 126 |
+
|
| 127 |
+
Chatbot = gr.Chatbot(
|
| 128 |
+
type="messages",
|
| 129 |
+
label="Anonymous User"
|
| 130 |
+
)
|
| 131 |
+
|
| 132 |
+
txt = gr.Textbox(
|
| 133 |
+
show_label=False,
|
| 134 |
+
placeholder=initial_message,
|
| 135 |
+
container=False
|
| 136 |
+
)
|
| 137 |
+
|
| 138 |
+
# Send message
|
| 139 |
+
txt.submit(lambda: "sending...", inputs=None, outputs=txt)
|
| 140 |
+
txt.submit(predict_prompt, inputs=[txt, user_id], outputs=Chatbot)
|
| 141 |
+
|
| 142 |
+
# Reset textbox after response
|
| 143 |
+
def clear():
|
| 144 |
+
return ""
|
| 145 |
+
|
| 146 |
+
Chatbot.change(clear, inputs=None, outputs=txt)
|
| 147 |
+
|
| 148 |
+
# =========================
|
| 149 |
+
# LAUNCH APP
|
| 150 |
+
# =========================
|
| 151 |
+
chatblock.launch()
|