Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
@@ -58,6 +58,9 @@ from fastapi import Request, Header
|
|
58 |
from fastapi import Body, Query
|
59 |
from fastapi.staticfiles import StaticFiles
|
60 |
from fastapi.templating import Jinja2Templates
|
|
|
|
|
|
|
61 |
|
62 |
import g4f
|
63 |
from g4f.client import Client
|
@@ -130,6 +133,40 @@ app = FastAPI(docs_url=None, redoc_url="/")
|
|
130 |
app.include_router(fluxai_router, prefix="/api/v1")
|
131 |
app.include_router(whisper_router, prefix="/api/v1")
|
132 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
133 |
timeout = 100
|
134 |
|
135 |
contact_support = """
|
|
|
58 |
from fastapi import Body, Query
|
59 |
from fastapi.staticfiles import StaticFiles
|
60 |
from fastapi.templating import Jinja2Templates
|
61 |
+
from fastapi import FastAPI, File, UploadFile
|
62 |
+
from fastapi.responses import FileResponse, JSONResponse
|
63 |
+
import uuid
|
64 |
|
65 |
import g4f
|
66 |
from g4f.client import Client
|
|
|
133 |
app.include_router(fluxai_router, prefix="/api/v1")
|
134 |
app.include_router(whisper_router, prefix="/api/v1")
|
135 |
|
136 |
+
|
137 |
+
UPLOAD_DIR = "./uploads"
|
138 |
+
|
139 |
+
@app.post("/uploadfile/")
|
140 |
+
async def upload_file(file: UploadFile = File(...)):
|
141 |
+
try:
|
142 |
+
ext = file.filename.split(".")[-1]
|
143 |
+
unique_filename = f"{uuid.uuid4().hex}.{ext}"
|
144 |
+
file_location = os.path.join(UPLOAD_DIR, unique_filename)
|
145 |
+
|
146 |
+
with open(file_location, "wb") as f:
|
147 |
+
f.write(await file.read())
|
148 |
+
|
149 |
+
return JSONResponse(
|
150 |
+
status_code=200,
|
151 |
+
content={"url": f"https://{os.getenv('SPACE_ID')}.hf.space/uploads/{unique_filename}"}
|
152 |
+
)
|
153 |
+
except Exception as e:
|
154 |
+
return JSONResponse(
|
155 |
+
status_code=500,
|
156 |
+
content={"error": str(e)}
|
157 |
+
)
|
158 |
+
|
159 |
+
@app.get("/uploads/{filename}")
|
160 |
+
async def serve_file(filename: str):
|
161 |
+
file_location = os.path.join(UPLOAD_DIR, filename)
|
162 |
+
if os.path.exists(file_location):
|
163 |
+
return FileResponse(file_location)
|
164 |
+
return JSONResponse(
|
165 |
+
status_code=404,
|
166 |
+
content={"error": "File not found"}
|
167 |
+
)
|
168 |
+
|
169 |
+
|
170 |
timeout = 100
|
171 |
|
172 |
contact_support = """
|