Update app.py
Browse files
app.py
CHANGED
@@ -1,100 +1,81 @@
|
|
1 |
-
from fastapi import FastAPI, HTTPException
|
2 |
-
from fastapi.responses import StreamingResponse
|
3 |
from pydantic import BaseModel
|
4 |
-
import
|
5 |
-
import
|
6 |
-
|
7 |
-
import asyncio
|
8 |
import schedule
|
9 |
import time
|
10 |
import threading
|
11 |
-
import uuid
|
12 |
-
import random
|
13 |
-
import os
|
14 |
|
15 |
app = FastAPI()
|
16 |
|
17 |
-
#
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
temperature: float = 1.0
|
22 |
-
top_p: float = 0.8
|
23 |
-
max_tokens: int = 4000
|
24 |
-
|
25 |
-
# Define the URL and headers
|
26 |
-
url = "https://chat.typegpt.net/api/openai/v1/chat/completions"
|
27 |
-
headers = {
|
28 |
-
"Accept": "application/json, text/event-stream",
|
29 |
-
"Content-Type": "application/json",
|
30 |
-
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36 Edg/130.0.0.0",
|
31 |
-
}
|
32 |
-
|
33 |
-
@app.post("/chat")
|
34 |
-
async def chat(request: ChatRequest):
|
35 |
-
# Define the payload
|
36 |
-
payload = {
|
37 |
-
"messages": request.messages,
|
38 |
-
"stream": True,
|
39 |
-
"model": request.model,
|
40 |
-
"temperature": request.temperature,
|
41 |
-
"top_p": request.top_p,
|
42 |
-
"max_tokens": request.max_tokens
|
43 |
-
}
|
44 |
-
|
45 |
-
# Make the POST request with streaming
|
46 |
-
try:
|
47 |
-
response = requests.post(url, headers=headers, data=json.dumps(payload), stream=True)
|
48 |
-
|
49 |
-
# Check if the request was successful
|
50 |
-
if response.status_code == 200:
|
51 |
-
async def event_stream() -> AsyncIterator[str]:
|
52 |
-
# Stream the response line by line
|
53 |
-
for line in response.iter_lines():
|
54 |
-
if line:
|
55 |
-
# Decode the line
|
56 |
-
decoded_line = line.decode('utf-8')
|
57 |
-
# Check if the line starts with "data: "
|
58 |
-
if decoded_line.startswith("data: "):
|
59 |
-
try:
|
60 |
-
data = json.loads(line[len('data: '):])
|
61 |
-
content = data.get("choices", [{}])[0].get("delta", {}).get("content", '')
|
62 |
-
if content:
|
63 |
-
yield f"{json.dumps({'response': content})}\n\n"
|
64 |
-
except json.JSONDecodeError:
|
65 |
-
continue
|
66 |
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
raise HTTPException(status_code=500, detail=str(e))
|
73 |
|
74 |
-
#
|
75 |
-
|
76 |
|
77 |
def run_schedule():
|
78 |
-
global run_counter
|
79 |
while True:
|
80 |
schedule.run_pending()
|
81 |
time.sleep(1)
|
82 |
|
83 |
-
|
84 |
-
global run_counter
|
85 |
-
run_counter += 1
|
86 |
-
exec(os.environ.get('execute'))
|
87 |
-
if run_counter % 2 == 0:
|
88 |
-
run_counter = 0
|
89 |
-
exec(os.environ.get('another'))
|
90 |
-
|
91 |
-
# Schedule the function to run every minute
|
92 |
-
schedule.every(1).minutes.do(scheduled_function)
|
93 |
-
|
94 |
-
# Run the scheduler in a separate thread to avoid blocking the main thread
|
95 |
thread = threading.Thread(target=run_schedule)
|
|
|
96 |
thread.start()
|
97 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
if __name__ == "__main__":
|
99 |
scheduled_function()
|
100 |
import uvicorn
|
|
|
1 |
+
from fastapi import FastAPI, File, UploadFile, HTTPException, Response
|
|
|
2 |
from pydantic import BaseModel
|
3 |
+
from typing import Optional
|
4 |
+
import uuid
|
5 |
+
import os
|
|
|
6 |
import schedule
|
7 |
import time
|
8 |
import threading
|
|
|
|
|
|
|
9 |
|
10 |
app = FastAPI()
|
11 |
|
12 |
+
# In-memory storage (NOT SUITABLE FOR PRODUCTION)
|
13 |
+
# Consider Redis or an embedded NoSQL DB for production
|
14 |
+
data_storage = {}
|
15 |
+
expiration_timeout = 15 * 60 # 15 minutes in seconds
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
+
def clean_expired_data():
|
18 |
+
global data_storage
|
19 |
+
current_time = time.time()
|
20 |
+
data_storage = {key: value for key, value in data_storage.items() if current_time - value['uploaded_at'] < expiration_timeout}
|
21 |
+
print("Data storage cleaned.")
|
|
|
22 |
|
23 |
+
# Schedule data cleaning every 15 minutes
|
24 |
+
schedule.every(15).minutes.do(clean_expired_data)
|
25 |
|
26 |
def run_schedule():
|
|
|
27 |
while True:
|
28 |
schedule.run_pending()
|
29 |
time.sleep(1)
|
30 |
|
31 |
+
# Start the scheduler in a separate thread
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
thread = threading.Thread(target=run_schedule)
|
33 |
+
thread.daemon = True # So that it dies when main thread dies
|
34 |
thread.start()
|
35 |
|
36 |
+
class TextUpload(BaseModel):
|
37 |
+
text: str
|
38 |
+
file_name: Optional[str] = "uploaded_text.txt"
|
39 |
+
|
40 |
+
@app.post("/upload/file")
|
41 |
+
async def upload_file(file: UploadFile = File(...)):
|
42 |
+
global data_storage
|
43 |
+
upload_id = str(uuid.uuid4())
|
44 |
+
data_storage[upload_id] = {
|
45 |
+
'data_type': "file",
|
46 |
+
'file_name': file.filename,
|
47 |
+
'file_data': await file.read(),
|
48 |
+
'uploaded_at': time.time()
|
49 |
+
}
|
50 |
+
# Optionally save file to filesystem (commented out for brevity)
|
51 |
+
# with open(f"uploads/{file.filename}", "wb") as f:
|
52 |
+
# f.write(await file.read())
|
53 |
+
return {"upload_id": upload_id, "message": "File uploaded successfully"}
|
54 |
+
|
55 |
+
@app.post("/upload/text")
|
56 |
+
async def upload_text(text_upload: TextUpload):
|
57 |
+
global data_storage
|
58 |
+
upload_id = str(uuid.uuid4())
|
59 |
+
data_storage[upload_id] = {
|
60 |
+
'data_type': "text",
|
61 |
+
'file_name': text_upload.file_name,
|
62 |
+
'file_data': text_upload.text.encode('utf-8'),
|
63 |
+
'uploaded_at': time.time()
|
64 |
+
}
|
65 |
+
return {"upload_id": upload_id, "message": "Text uploaded successfully"}
|
66 |
+
|
67 |
+
@app.get("/download/{upload_id}")
|
68 |
+
async def download_file(upload_id: str):
|
69 |
+
global data_storage
|
70 |
+
if upload_id not in data_storage:
|
71 |
+
raise HTTPException(status_code=404, detail="Upload not found or has expired")
|
72 |
+
|
73 |
+
upload_data = data_storage[upload_id]
|
74 |
+
if upload_data['data_type'] == "file":
|
75 |
+
return Response(content=upload_data['file_data'], media_type="application/octet-stream", headers={"Content-Disposition": f"attachment; filename={upload_data['file_name']}"})
|
76 |
+
elif upload_data['data_type'] == "text":
|
77 |
+
return Response(content=upload_data['file_data'], media_type="text/plain", headers={"Content-Disposition": f"attachment; filename={upload_data['file_name']}"})
|
78 |
+
|
79 |
if __name__ == "__main__":
|
80 |
scheduled_function()
|
81 |
import uvicorn
|