Skip to content

Commit c69bd4b

Browse files
committed
Added the Fast API support (compatibility with OpenAI integrations)
1 parent d7c9cab commit c69bd4b

File tree

4 files changed

+51
-4
lines changed

4 files changed

+51
-4
lines changed

src/FreeGPT4_Server.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
import string
77
import sqlite3
88
from uuid import uuid4
9+
import threading
910

1011
# GPT Library
1112
import g4f
13+
from g4f.api import run_api
1214

1315
# Server
1416
from flask import Flask, redirect, render_template
@@ -155,6 +157,12 @@
155157
type=str,
156158
help="Use a system prompt to 'customize' the answers",
157159
)
160+
parser.add_argument(
161+
"--enable-fast-api",
162+
action='store_true',
163+
required=False,
164+
help="Use the fast API standard (PORT 1337 - compatible with OpenAI integrations)",
165+
)
158166

159167
args, unknown = parser.parse_known_args()
160168

@@ -171,6 +179,8 @@
171179
else:
172180
proxies = None
173181

182+
# Crates the Fast API Thread
183+
t = threading.Thread(target=run_api,name="fastapi")
174184

175185
# Loads the settings from the file
176186
def load_settings(file=SETTINGS_FILE):
@@ -192,11 +202,17 @@ def load_settings(file=SETTINGS_FILE):
192202
"system_prompt": data[9],
193203
"message_history": data[10],
194204
"proxies": data[11],
195-
"password": data[12]
205+
"password": data[12],
206+
"fast_api": data[13]
196207
}
197208
print(data)
198209
return data
199210

211+
def start_fast_api():
212+
print("[!] FAST API PORT: 1336")
213+
t.daemon = True
214+
t.start()
215+
200216
# Updates the settings
201217
data = load_settings()
202218
if (args.keyword == None):
@@ -236,6 +252,9 @@ def load_settings(file=SETTINGS_FILE):
236252
if (args.enable_proxies == False):
237253
args.enable_proxies = data["proxies"]
238254

255+
if (args.enable_fast_api or data["fast_api"]):
256+
start_fast_api()
257+
239258

240259
# Initializes the message history
241260
message_history = [{"role": "system", "content": args.system_prompt}]
@@ -296,6 +315,7 @@ def save_settings(request, file):
296315
c.execute("UPDATE settings SET system_prompt = ?", (request.form["system_prompt"],))
297316
c.execute("UPDATE settings SET message_history = ?", (bool(request.form["message_history"] == "true"),))
298317
c.execute("UPDATE settings SET proxies = ?", (bool(request.form["proxies"] == "true"),))
318+
c.execute("UPDATE settings SET fast_api = ?", (bool(request.form["fast_api"] == "true"),))
299319
if (len(request.form["new_password"]) > 0):
300320
c.execute("UPDATE settings SET password = ?", (generate_password_hash(request.form["new_password"]),))
301321

@@ -345,6 +365,9 @@ def save_settings(request, file):
345365
json.dump(proxies, pf)
346366
pf.close()
347367

368+
if (bool(request.form["fast_api"] == "true") and not args.enable_fast_api):
369+
start_fast_api()
370+
348371
conn.commit()
349372
conn.close()
350373
return
@@ -364,6 +387,7 @@ def apply_settings(file):
364387
args.enable_history = data["message_history"]
365388
args.enable_proxies = data["proxies"]
366389
args.password = data["password"]
390+
args.enable_fast_api = data["fast_api"]
367391
return
368392

369393

src/data/settings.db

0 Bytes
Binary file not shown.

src/requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ g4f
77
Werkzeug>=3.0.3
88
aiohttp_socks
99
nodriver
10-
pysqlite3
10+
pysqlite3
11+
python-multipart

src/templates/settings.html

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,15 +154,37 @@ <h1 class="title font-bold inter darkblue text-4xl">FreeGPT4</h1>
154154
{% endif %}
155155
</td>
156156
</tr>
157+
<tr>
158+
<td class="py-1 fond-bold inter darkblue text-lg border-b border-slate-800" id="set_5">
159+
<b>Fast API:</b>
160+
<br>
161+
<p id="warning_2" class="text-sm label_darkorange" hidden> <b>Warning:</b> the Fast API shutdown will take effect only after you've manually restarted the server </p>
162+
</td>
163+
<td class="py-1 fond-bold inter darkblue text-lg">
164+
{% if data['fast_api'] %}
165+
<input type="radio" id="off_rad_5" name="fast_api" value="false" hidden oninput="turnOff('on_5', 'off_5', 'set_5'); showElement('warning_2');">
166+
<label for="off_rad_5" id="off_5" class="label_gray"><b>OFF</b></label>
167+
<p class="inline-block mx-1"> - </p>
168+
<input type="radio" id="on_rad_5" name="fast_api" value="true" hidden oninput="turnOn('on_5', 'off_5', 'set_5'); hideElement('warning_2');" checked>
169+
<label for="on_rad_5" id="on_5" class="label_green"><b>ON</b></label>
170+
{% else %}
171+
<input type="radio" id="off_rad_5" name="fast_api" value="false" hidden oninput="turnOff('on_5', 'off_5', 'set_5'); showElement('warning_2');" checked>
172+
<label for="off_rad_5" id="off_5" class="label_red"><b>OFF</b></label>
173+
<p class="inline-block mx-1"> - </p>
174+
<input type="radio" id="on_rad_5" name="fast_api" value="true" hidden oninput="turnOn('on_5', 'off_5', 'set_5'); hideElement('warning_2');">
175+
<label for="on_rad_5" id="on_5" class="label_gray"><b>ON</b></label>
176+
{% endif %}
177+
</td>
178+
</tr>
157179
<tr>
158180
<td class="py-1 fond-bold inter darkblue text-lg border-b border-slate-800">
159181
<b>Server port:</b>
160182
<br>
161-
<p id="warning_2" class="text-sm label_darkorange" hidden> <b>Warning:</b> the port change will take effect only after you've manually restarted the server </p>
183+
<p id="warning_3" class="text-sm label_darkorange" hidden> <b>Warning:</b> the port change will take effect only after you've manually restarted the server </p>
162184
</td>
163185
<td class="py-1 fond-bold inter darkblue text-lg">
164186
<b>
165-
<input type="number" id="port" name="port" class="input outline-none py-1 px-2 rounded-lg inter w-24" placeholder="Port" value="{{ data['port'] }}" onchange="showElement('warning_2')">
187+
<input type="number" id="port" name="port" class="input outline-none py-1 px-2 rounded-lg inter w-24" placeholder="Port" value="{{ data['port'] }}" onchange="showElement('warning_3')">
166188
</b>
167189
</td>
168190
</tr>

0 commit comments

Comments
 (0)