Update app.py
Browse files
app.py
CHANGED
@@ -1,30 +1,160 @@
|
|
1 |
-
from fastapi import FastAPI,
|
2 |
-
from
|
3 |
import os
|
|
|
|
|
|
|
4 |
|
5 |
app = FastAPI()
|
6 |
|
7 |
-
#
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
#
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, Form, Request
|
2 |
+
from fastapi.responses import HTMLResponse, StreamingResponse
|
3 |
import os
|
4 |
+
import json
|
5 |
+
import requests
|
6 |
+
import random
|
7 |
|
8 |
app = FastAPI()
|
9 |
|
10 |
+
# Load environment variables
|
11 |
+
MODEL = "gpt-4o-mini"
|
12 |
+
API_URL = os.getenv("API_URL")
|
13 |
+
DISABLED = os.getenv("DISABLED") == 'True'
|
14 |
+
OPENAI_API_KEYS = os.getenv("OPENAI_API_KEYS", "").split(",")
|
15 |
+
NUM_THREADS = int(os.getenv("NUM_THREADS", 1)) # Default to 1 if not set
|
16 |
+
|
17 |
+
# HTML with embedded CSS and JS
|
18 |
+
HTML_CONTENT = """
|
19 |
+
<!DOCTYPE html>
|
20 |
+
<html lang="en">
|
21 |
+
<head>
|
22 |
+
<meta charset="UTF-8">
|
23 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
24 |
+
<title>GPT-4o Mini Chat</title>
|
25 |
+
<style>
|
26 |
+
body { font-family: Arial, sans-serif; background-color: #f4f4f4; margin: 0; padding: 20px; }
|
27 |
+
.container { max-width: 800px; margin: auto; background: white; padding: 20px; border-radius: 8px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); }
|
28 |
+
h1 { color: #333; text-align: center; }
|
29 |
+
.chatbox { height: 400px; overflow-y: auto; border: 1px solid #ccc; padding: 10px; margin-bottom: 20px; }
|
30 |
+
.message { margin: 10px 0; padding: 8px; border-radius: 5px; }
|
31 |
+
.user { background: #e6f3ff; }
|
32 |
+
.assistant { background: #f0f0f0; }
|
33 |
+
form { display: flex; flex-direction: column; gap: 10px; }
|
34 |
+
input, select, button { padding: 8px; font-size: 16px; }
|
35 |
+
button { background-color: #007bff; color: white; border: none; border-radius: 5px; cursor: pointer; }
|
36 |
+
button:hover { background-color: #0056b3; }
|
37 |
+
button:disabled { background-color: #ccc; cursor: not-allowed; }
|
38 |
+
</style>
|
39 |
+
</head>
|
40 |
+
<body>
|
41 |
+
<div class="container">
|
42 |
+
<h1>GPT-4o Mini: Research Preview</h1>
|
43 |
+
<div id="chatbox" class="chatbox"></div>
|
44 |
+
<form id="chat-form" action="/chat" method="post">
|
45 |
+
<input type="text" id="input" name="input" placeholder="Type your message..." required>
|
46 |
+
<select name="top_p">
|
47 |
+
<option value="1.0">Top P: 1.0</option>
|
48 |
+
<option value="0.9">Top P: 0.9</option>
|
49 |
+
<option value="0.8">Top P: 0.8</option>
|
50 |
+
</select>
|
51 |
+
<select name="temperature">
|
52 |
+
<option value="1.0">Temperature: 1.0</option>
|
53 |
+
<option value="0.7">Temperature: 0.7</option>
|
54 |
+
<option value="0.3">Temperature: 0.3</option>
|
55 |
+
</select>
|
56 |
+
<button type="submit" id="submit-btn">Send</button>
|
57 |
+
</form>
|
58 |
+
</div>
|
59 |
+
<script>
|
60 |
+
const chatbox = document.getElementById("chatbox");
|
61 |
+
const form = document.getElementById("chat-form");
|
62 |
+
const input = document.getElementById("input");
|
63 |
+
const submitBtn = document.getElementById("submit-btn");
|
64 |
+
let history = JSON.parse(localStorage.getItem("chatHistory")) || [];
|
65 |
+
|
66 |
+
// Load existing history
|
67 |
+
history.forEach(msg => addMessage(msg.role, msg.content));
|
68 |
+
|
69 |
+
form.addEventListener("submit", async (e) => {
|
70 |
+
e.preventDefault();
|
71 |
+
const userInput = input.value;
|
72 |
+
const topP = form.top_p.value;
|
73 |
+
const temperature = form.temperature.value;
|
74 |
+
|
75 |
+
addMessage("user", userInput);
|
76 |
+
input.value = "";
|
77 |
+
submitBtn.disabled = true;
|
78 |
+
|
79 |
+
const response = await fetch("/chat", {
|
80 |
+
method: "POST",
|
81 |
+
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
82 |
+
body: `input=${encodeURIComponent(userInput)}&top_p=${topP}&temperature=${temperature}`
|
83 |
+
});
|
84 |
+
|
85 |
+
const reader = response.body.getReader();
|
86 |
+
let assistantMessage = "";
|
87 |
+
const decoder = new TextDecoder();
|
88 |
+
|
89 |
+
while (true) {
|
90 |
+
const { done, value } = await reader.read();
|
91 |
+
if (done) break;
|
92 |
+
assistantMessage += decoder.decode(value);
|
93 |
+
updateLastMessage("assistant", assistantMessage);
|
94 |
+
}
|
95 |
+
|
96 |
+
history.push({ role: "user", content: userInput }, { role: "assistant", content: assistantMessage });
|
97 |
+
localStorage.setItem("chatHistory", JSON.stringify(history));
|
98 |
+
submitBtn.disabled = false;
|
99 |
+
});
|
100 |
+
|
101 |
+
function addMessage(role, content) {
|
102 |
+
const div = document.createElement("div");
|
103 |
+
div.className = `message ${role}`;
|
104 |
+
div.textContent = content;
|
105 |
+
chatbox.appendChild(div);
|
106 |
+
chatbox.scrollTop = chatbox.scrollHeight;
|
107 |
+
}
|
108 |
+
|
109 |
+
function updateLastMessage(role, content) {
|
110 |
+
const lastMsg = chatbox.lastElementChild;
|
111 |
+
if (lastMsg && lastMsg.className.includes(role)) {
|
112 |
+
lastMsg.textContent = content;
|
113 |
+
} else {
|
114 |
+
addMessage(role, content);
|
115 |
+
}
|
116 |
+
chatbox.scrollTop = chatbox.scrollHeight;
|
117 |
+
}
|
118 |
+
</script>
|
119 |
+
</body>
|
120 |
+
</html>
|
121 |
+
"""
|
122 |
+
|
123 |
+
# Serve the chat interface
|
124 |
+
@app.get("/", response_class=HTMLResponse)
|
125 |
+
async def home():
|
126 |
+
if DISABLED:
|
127 |
+
return "<h1 style='color:red;text-align:center'>This app has reached OpenAI's usage limit. Please check back tomorrow.</h1>"
|
128 |
+
return HTML_CONTENT
|
129 |
+
|
130 |
+
# Handle chat input and stream response
|
131 |
+
@app.post("/chat")
|
132 |
+
async def chat(input: str = Form(...), top_p: float = Form(1.0), temperature: float = Form(1.0)):
|
133 |
+
if DISABLED:
|
134 |
+
return StreamingResponse(iter(["Usage limit reached."]), media_type="text/plain")
|
135 |
+
|
136 |
+
payload = {
|
137 |
+
"model": MODEL,
|
138 |
+
"messages": [{"role": "user", "content": input}],
|
139 |
+
"temperature": temperature,
|
140 |
+
"top_p": top_p,
|
141 |
+
"n": 1,
|
142 |
+
"stream": True,
|
143 |
+
"presence_penalty": 0,
|
144 |
+
"frequency_penalty": 0,
|
145 |
+
}
|
146 |
+
|
147 |
+
OPENAI_API_KEY = random.choice(OPENAI_API_KEYS)
|
148 |
+
headers = {
|
149 |
+
"Content-Type": "application/json",
|
150 |
+
"Authorization": f"Bearer {OPENAI_API_KEY}",
|
151 |
+
}
|
152 |
+
|
153 |
+
def stream_response():
|
154 |
+
try:
|
155 |
+
response = requests.post(API_URL, headers=headers, json=payload, stream=True)
|
156 |
+
response.raise_for_status()
|
157 |
+
for chunk in response.iter_lines():
|
158 |
+
if chunk:
|
159 |
+
chunk_data = chunk.decode('utf-8')
|
160 |
+
if chunk_data.startswith(
|