Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- Dockerfile +16 -0
- app.py +58 -0
- requirements.txt +6 -0
Dockerfile
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
|
2 |
+
# you will also find guides on how best to write your Dockerfile
|
3 |
+
|
4 |
+
FROM python:3.9
|
5 |
+
|
6 |
+
RUN useradd -m -u 1000 user
|
7 |
+
USER user
|
8 |
+
ENV PATH="/home/user/.local/bin:$PATH"
|
9 |
+
|
10 |
+
WORKDIR /app
|
11 |
+
|
12 |
+
COPY --chown=user ./requirements.txt requirements.txt
|
13 |
+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
14 |
+
|
15 |
+
COPY --chown=user . /app
|
16 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, UploadFile, File
|
2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
3 |
+
import google.generativeai as genai
|
4 |
+
from PIL import Image
|
5 |
+
import io
|
6 |
+
import os
|
7 |
+
from dotenv import load_dotenv
|
8 |
+
|
9 |
+
|
10 |
+
load_dotenv()
|
11 |
+
|
12 |
+
|
13 |
+
app = FastAPI(title="Simple Image Description API")
|
14 |
+
|
15 |
+
|
16 |
+
origins = ["*"]
|
17 |
+
|
18 |
+
|
19 |
+
app.add_middleware(
|
20 |
+
CORSMiddleware,
|
21 |
+
allow_origins=origins,
|
22 |
+
allow_credentials=True,
|
23 |
+
allow_methods=["*"],
|
24 |
+
allow_headers=["*"],
|
25 |
+
)
|
26 |
+
|
27 |
+
|
28 |
+
genai.configure(api_key=os.getenv("GOOGLE_GEMINI_API_KEY"))
|
29 |
+
|
30 |
+
|
31 |
+
model = genai.GenerativeModel('gemini-1.5-flash')
|
32 |
+
|
33 |
+
|
34 |
+
@app.post("/describe-image/")
|
35 |
+
async def describe_image(imageInputByUser: UploadFile = File(...)):
|
36 |
+
|
37 |
+
try:
|
38 |
+
|
39 |
+
image_content = await imageInputByUser.read()
|
40 |
+
|
41 |
+
|
42 |
+
img = Image.open(io.BytesIO(image_content))
|
43 |
+
|
44 |
+
|
45 |
+
response = model.generate_content(["Please provide a short description of this image", img])
|
46 |
+
|
47 |
+
|
48 |
+
return {
|
49 |
+
'success': True,
|
50 |
+
'message': response.text
|
51 |
+
}
|
52 |
+
|
53 |
+
except Exception as e:
|
54 |
+
|
55 |
+
return {
|
56 |
+
'success': False,
|
57 |
+
'message': str(e)
|
58 |
+
}
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
fastapi==0.109.2
|
2 |
+
Pillow==11.0.0
|
3 |
+
python-dotenv==1.0.1
|
4 |
+
python-multipart==0.0.9
|
5 |
+
google-generativeai==0.7.1
|
6 |
+
uvicorn==0.27.1
|