Tabatha-625 commited on
Commit
e329bf7
·
verified ·
1 Parent(s): b7a7514

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +240 -0
app.py CHANGED
@@ -1,3 +1,119 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  @app.post("/rename-chat")
2
  def rename_chat(data: dict):
3
  supabase.table("conversations") \
@@ -5,3 +121,127 @@ def rename_chat(data: dict):
5
  .eq("id", data["id"]) \
6
  .execute()
7
  return {"ok": True}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+
3
+ def load_history(user):
4
+ try:
5
+ with open(f"{user}.json", "r") as f:
6
+ return json.load(f)
7
+ except:
8
+ return []
9
+
10
+ def save_history(user, history):
11
+ with open(f"{user}.json", "w") as f:
12
+ json.dump(history, f)
13
+ import gradio as gr
14
+ from openai import OpenAI
15
+ import os
16
+
17
+
18
+ client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
19
+
20
+ USER_SESSIONS = {}
21
+
22
+ def respond(username, message, history):
23
+ if username not in USER_SESSIONS:
24
+ USER_SESSIONS[username] = []
25
+
26
+ history = USER_SESSIONS[username]
27
+
28
+
29
+
30
+
31
+
32
+ messages = [{"role": "system", "content": "You are a helpful assistant."}]
33
+ for u, b in history:
34
+ messages.append({"role": "user", "content": u})
35
+ messages.append({"role": "assistant", "content": b})
36
+ messages.append({"role": "user", "content": message})
37
+
38
+ response = client.chat.completions.create(
39
+ model="gpt-4o-mini",
40
+ messages=messages
41
+ )
42
+
43
+ reply = response.choices[0].message.content
44
+ history.append((message, reply))
45
+ USER_SESSIONS[username] = history
46
+
47
+ return "", history
48
+
49
+ with gr.Blocks() as demo:
50
+ gr.Markdown("## 🔐 Login")
51
+ username = gr.Textbox(label="Username")
52
+
53
+ chatbot = gr.Chatbot(height=400)
54
+ msg = gr.Textbox(placeholder="Type your message…", show_label=False)
55
+
56
+ msg.submit(respond, [username, msg, chatbot], [msg, chatbot])
57
+
58
+ demo.launch()
59
+ from fastapi import FastAPI
60
+ from fastapi.responses import FileResponse
61
+
62
+
63
+
64
+
65
+
66
+
67
+
68
+
69
+
70
+
71
+
72
+ from openai import OpenAI
73
+ import os
74
+
75
+ app = FastAPI()
76
+ client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
77
+
78
+ @app.get("/")
79
+ def root():
80
+ return FileResponse("static/index.html")
81
+
82
+
83
+
84
+ @app.post("/chat")
85
+ async def chat(data: dict):
86
+ messages = [
87
+ {"role": "system", "content": "You are ChatGPT. Be helpful and concise."}
88
+ ] + data["messages"]
89
+
90
+ res = client.chat.completions.create(
91
+
92
+
93
+
94
+
95
+
96
+
97
+ model="gpt-4o-mini",
98
+ messages=messages
99
+ )
100
+
101
+ return res.choices[0].message
102
+
103
+
104
+
105
+
106
+
107
+
108
+
109
+
110
+
111
+
112
+
113
+
114
+
115
+
116
+
117
  @app.post("/rename-chat")
118
  def rename_chat(data: dict):
119
  supabase.table("conversations") \
 
121
  .eq("id", data["id"]) \
122
  .execute()
123
  return {"ok": True}
124
+
125
+ @app.post("/rename-chat")
126
+ def rename_chat(data: dict):
127
+ supabase.table("conversations") \
128
+ .update({"title": data["title"]}) \
129
+ .eq("id", data["id"]) \
130
+ .execute()
131
+ return {"ok": True}
132
+
133
+
134
+
135
+ onDoubleClick={() => {
136
+ const title = prompt("Rename chat");
137
+ fetch("/rename-chat", {
138
+ method: "POST",
139
+ headers: {"Content-Type": "application/json"},
140
+ body: JSON.stringify({ id: chat.id, title })
141
+ });
142
+ }}
143
+
144
+
145
+
146
+
147
+
148
+
149
+
150
+
151
+
152
+
153
+
154
+
155
+
156
+
157
+
158
+
159
+
160
+
161
+
162
+
163
+
164
+
165
+
166
+
167
+
168
+
169
+
170
+
171
+
172
+
173
+
174
+
175
+
176
+
177
+
178
+
179
+
180
+
181
+
182
+
183
+
184
+
185
+
186
+
187
+
188
+
189
+
190
+
191
+
192
+
193
+
194
+
195
+
196
+
197
+
198
+
199
+
200
+
201
+
202
+
203
+
204
+
205
+
206
+
207
+
208
+
209
+
210
+
211
+
212
+
213
+
214
+
215
+
216
+
217
+
218
+
219
+
220
+
221
+
222
+
223
+
224
+
225
+
226
+
227
+
228
+
229
+
230
+
231
+
232
+
233
+
234
+
235
+
236
+
237
+
238
+
239
+
240
+
241
+
242
+
243
+
244
+
245
+
246
+
247
+