Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,58 +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 |
-
}
|
|
|
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 |
+
}
|