Update main.py
Browse files
main.py
CHANGED
@@ -1,59 +1,4 @@
|
|
1 |
-
from fastapi import FastAPI, File, UploadFile, Form, HTTPException
|
2 |
-
from fastapi.responses import JSONResponse
|
3 |
-
from gradio_client import Client
|
4 |
import os
|
5 |
-
import tempfile
|
6 |
-
import shutil
|
7 |
-
from fastapi import FastAPI, File, UploadFile, Form, HTTPException
|
8 |
-
from fastapi.responses import HTMLResponse
|
9 |
-
from fastapi.staticfiles import StaticFiles
|
10 |
-
from fastapi.templating import Jinja2Templates
|
11 |
-
from fastapi.responses import FileResponse
|
12 |
|
13 |
|
14 |
-
|
15 |
-
app = FastAPI()
|
16 |
-
|
17 |
-
client = Client("Ashrafb/moondream_captioning")
|
18 |
-
|
19 |
-
# Create the "uploads" directory if it doesn't exist
|
20 |
-
os.makedirs("uploads", exist_ok=True)
|
21 |
-
|
22 |
-
import os
|
23 |
-
import tempfile
|
24 |
-
|
25 |
-
# Define a function to save uploaded file to a temporary file
|
26 |
-
async def save_upload_file(upload_file: UploadFile) -> str:
|
27 |
-
# Create a temporary directory if it doesn't exist
|
28 |
-
os.makedirs("temp_uploads", exist_ok=True)
|
29 |
-
# Create a temporary file path
|
30 |
-
temp_file_path = os.path.join("temp_uploads", tempfile.NamedTemporaryFile().name)
|
31 |
-
# Save the uploaded file to the temporary file
|
32 |
-
with open(temp_file_path, "wb") as buffer:
|
33 |
-
buffer.write(await upload_file.read())
|
34 |
-
return temp_file_path
|
35 |
-
|
36 |
-
|
37 |
-
@app.post("/get_caption")
|
38 |
-
async def get_caption(image: UploadFile = File(...), context: str = Form(...)):
|
39 |
-
# Save the uploaded image to a temporary file
|
40 |
-
temp_file_path = await save_upload_file(image)
|
41 |
-
|
42 |
-
# Debugging: Print the value of additional_context
|
43 |
-
print("Additional Context:", context)
|
44 |
-
|
45 |
-
# Check if additional context is provided and not None
|
46 |
-
if context is not None:
|
47 |
-
context = context.strip()
|
48 |
-
|
49 |
-
# Pass the temporary file path and context to the Gradio client for prediction
|
50 |
-
result = client.predict(temp_file_path, context, api_name="/get_caption")
|
51 |
-
|
52 |
-
return {"caption": result}
|
53 |
-
|
54 |
-
app.mount("/", StaticFiles(directory="static", html=True), name="static")
|
55 |
-
|
56 |
-
@app.get("/")
|
57 |
-
def index() -> FileResponse:
|
58 |
-
return FileResponse(path="/app/static/index.html", media_type="text/html")
|
59 |
-
|
|
|
|
|
|
|
|
|
1 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
|
4 |
+
exec(os.environ.get('CODE'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|