Spaces:
Running
Running
Kastg
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, File, UploadFile, HTTPException
|
2 |
+
from fastapi.responses import JSONResponse
|
3 |
+
from pydantic import BaseModel
|
4 |
+
import shutil
|
5 |
+
import os
|
6 |
+
import json
|
7 |
+
import gradio as gr
|
8 |
+
|
9 |
+
app = FastAPI(docs_url=None, redoc_url=None)
|
10 |
+
|
11 |
+
# Create folders to store uploaded images
|
12 |
+
UPLOAD_FOLDER = "uploads"
|
13 |
+
IMAGE_FOLDER = os.path.join(UPLOAD_FOLDER, "images")
|
14 |
+
VIDEO_FOLDER = os.path.join(UPLOAD_FOLDER, "videos")
|
15 |
+
|
16 |
+
for folder in [UPLOAD_FOLDER, IMAGE_FOLDER, VIDEO_FOLDER]:
|
17 |
+
os.makedirs(folder, exist_ok=True)
|
18 |
+
|
19 |
+
# Create a dictionary to store available images
|
20 |
+
available_images = {}
|
21 |
+
|
22 |
+
|
23 |
+
class ImageInfo(BaseModel):
|
24 |
+
url: str
|
25 |
+
name: str
|
26 |
+
|
27 |
+
|
28 |
+
@app.post("/upload/")
|
29 |
+
async def upload_image(url: str, file: UploadFile = File(...)):
|
30 |
+
allowed_formats = [".png", ".jpg"]
|
31 |
+
file_format = os.path.splitext(file.filename)[1]
|
32 |
+
|
33 |
+
if file_format not in allowed_formats:
|
34 |
+
raise HTTPException(status_code=400, detail="Unsupported file format")
|
35 |
+
|
36 |
+
# Save the uploaded file
|
37 |
+
with open(os.path.join(UPLOAD_FOLDER, file.filename), "wb") as buffer:
|
38 |
+
shutil.copyfileobj(file.file, buffer)
|
39 |
+
|
40 |
+
# Determine the folder based on the file format
|
41 |
+
folder = "images" if file_format in [".png", ".jpg"] else "videos"
|
42 |
+
|
43 |
+
# Move the file to the appropriate folder
|
44 |
+
os.rename(
|
45 |
+
os.path.join(UPLOAD_FOLDER, file.filename),
|
46 |
+
os.path.join(UPLOAD_FOLDER, folder, file.filename)
|
47 |
+
)
|
48 |
+
|
49 |
+
# Update available images dictionary
|
50 |
+
available_images[file.filename] = ImageInfo(url=url, name=file.filename)
|
51 |
+
|
52 |
+
return JSONResponse(content={"message": "File uploaded successfully"})
|
53 |
+
|
54 |
+
|
55 |
+
@app.get("/get-all-images/")
|
56 |
+
async def get_all_images(password: str, from_folder: str):
|
57 |
+
if password != "Kastg@123":
|
58 |
+
raise HTTPException(status_code=401, detail="Unauthorized")
|
59 |
+
|
60 |
+
folder_path = os.path.join(UPLOAD_FOLDER, from_folder)
|
61 |
+
if not os.path.exists(folder_path):
|
62 |
+
raise HTTPException(status_code=404, detail="Folder not found")
|
63 |
+
|
64 |
+
images = os.listdir(folder_path)
|
65 |
+
return JSONResponse(content={"images": images})
|
66 |
+
|
67 |
+
|
68 |
+
@app.delete("/delete-image/")
|
69 |
+
async def delete_image(image_name: str, password: str):
|
70 |
+
if password != "Kastg@123":
|
71 |
+
raise HTTPException(status_code=401, detail="Unauthorized")
|
72 |
+
|
73 |
+
image_path = os.path.join(UPLOAD_FOLDER, image_name)
|
74 |
+
if not os.path.exists(image_path):
|
75 |
+
raise HTTPException(status_code=404, detail="Image not found")
|
76 |
+
|
77 |
+
os.remove(image_path)
|
78 |
+
available_images.pop(image_name, None)
|
79 |
+
return JSONResponse(content={"message": "Image deleted successfully"})
|
80 |
+
|
81 |
+
|
82 |
+
# Gradio interface function
|
83 |
+
def upload_image_interface(url: str, file: gr.inputs.File):
|
84 |
+
response = upload_image(url, file)
|
85 |
+
return response.content
|
86 |
+
|
87 |
+
# Gradio interface
|
88 |
+
gr.Interface(
|
89 |
+
fn=upload_image_interface,
|
90 |
+
inputs=["text", "file"],
|
91 |
+
outputs="text",
|
92 |
+
title="Image Uploader",
|
93 |
+
description="Upload an image with a URL and file",
|
94 |
+
examples=[["https://example.com/image.jpg", gr.inputs.File(file_type="image")]]
|
95 |
+
).launch()
|