randydev commited on
Commit
f6f280b
1 Parent(s): f32275f

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +31 -0
main.py CHANGED
@@ -29,6 +29,7 @@ import tempfile
29
  import io
30
  import string
31
  import openai
 
32
  from io import BytesIO
33
  from datetime import datetime as dt
34
  from dotenv import load_dotenv
@@ -147,6 +148,36 @@ Please modify your search terms and/or try again later thank you for your unders
147
  ~ @xtdevs Team
148
  """
149
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
150
  class FedBans(BaseModel):
151
  user_id: int
152
  hashtag: str
 
29
  import io
30
  import string
31
  import openai
32
+ import uuid
33
  from io import BytesIO
34
  from datetime import datetime as dt
35
  from dotenv import load_dotenv
 
148
  ~ @xtdevs Team
149
  """
150
 
151
+ UPLOAD_DIRECTORY = "./uploads"
152
+
153
+ @app.post("/uploadfile/")
154
+ async def upload_file(file: UploadFile = File(...)):
155
+ try:
156
+ ext = file.filename.split(".")[-1]
157
+ unique_filename = f"{uuid.uuid4().hex}.{ext}"
158
+ file_location = os.path.join(UPLOAD_DIRECTORY, unique_filename)
159
+ with open(file_location, "wb") as f:
160
+ f.write(await file.read())
161
+ return JSONResponse(
162
+ status_code=200,
163
+ content={"url": f"https://randydev-ryuzaki-api.hf.space/api/v1/uploads/{unique_filename}"}
164
+ )
165
+ except Exception as e:
166
+ return JSONResponse(
167
+ status_code=500,
168
+ content={"error": str(e)}
169
+ )
170
+
171
+ @app.get("/uploads/{filename}")
172
+ async def serve_file(filename: str):
173
+ file_location = os.path.join(UPLOAD_DIRECTORY, filename)
174
+ if os.path.exists(file_location):
175
+ return FileResponse(file_location)
176
+ return JSONResponse(
177
+ status_code=404,
178
+ content={"error": "File not found"}
179
+ )
180
+
181
  class FedBans(BaseModel):
182
  user_id: int
183
  hashtag: str