triflix commited on
Commit
d859282
·
verified ·
1 Parent(s): 4632e79

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +57 -104
main.py CHANGED
@@ -1,128 +1,81 @@
1
  import os
2
  import shutil
3
- from fastapi import FastAPI, File, UploadFile, HTTPException, Request
 
 
 
4
  from fastapi.responses import JSONResponse, HTMLResponse
5
  from fastapi.templating import Jinja2Templates
6
 
7
- # Import the required modules from google.genai
8
- from google import genai
9
- from google.genai import types
10
-
11
- app = FastAPI()
12
- templates = Jinja2Templates(directory="templates")
13
-
14
- def generate_question(image_path: str) -> str:
15
- # Initialize the client using your GEMINI_API_KEY environment variable.
16
- client = genai.Client(api_key=os.environ.get("GEMINI_API_KEY"))
17
-
18
- # Upload the image file
19
- files = [client.files.upload(file=image_path)]
20
- model = "gemini-2.0-flash"
21
-
22
- # Prepare the content using the image URI and provided sample text
23
- contents = [
24
- types.Content(
25
- role="user",
26
- parts=[
27
- types.Part.from_uri(
28
- file_uri=files[0].uri,
29
- mime_type=files[0].mime_type,
30
- ),
31
- ],
32
- ),
33
- types.Content(
34
- role="model",
35
- parts=[
36
- types.Part.from_text(text="""You are absolutely correct! My apologies for the repeated errors. The correct answer is 24 degrees.
37
-
38
- ```json
39
- {
40
- \"question\": \"This horizontal bar chart displays the average daily temperatures recorded for each day of the week. If the temperature on Thursday was increased by 1/4th times of the second highest temperature of the week, what temperature would be experienced on Thursday?\",
41
- \"options\": [
42
- \"a. 35°\",
43
- \"b. 24°\",
44
- \"c. 18°\",
45
- \"d. 30°\"
46
- ],
47
- \"answer\": \"b\"
48
- }
49
- ```"""),
50
- ],
51
- ),
52
- types.Content(
53
- role="user",
54
- parts=[
55
- types.Part.from_text(text="INSERT_INPUT_HERE"),
56
- ],
57
- ),
58
- ]
59
- tools = [types.Tool(function_declarations=[])]
60
- generate_content_config = types.GenerateContentConfig(
61
- temperature=1,
62
- top_p=0.95,
63
- top_k=40,
64
- max_output_tokens=8192,
65
- tools=tools,
66
- response_mime_type="text/plain",
67
- system_instruction=[
68
- types.Part.from_text(text="""analyse question , analyse option , and give me question , it's options and answer in json format
69
 
70
- fixed json format
 
 
 
71
 
72
- below is example of json format ,the json structure is same like below
 
73
 
74
- {
75
- \"question\": \"What is the capital of France?\",
76
- \"options\": [
77
- \"a. Berlin\",
78
- \"b. Madrid\",
79
- \"c. Paris\",
80
- \"d. Rome\"
81
- ],
82
- \"answer\": \"c\"
83
- }
84
 
85
- only json format nothing else
86
- """),
87
- ],
88
- )
89
 
90
- # Generate content stream and collect the response
91
- response_text = ""
92
- for chunk in client.models.generate_content_stream(
93
- model=model,
94
- contents=contents,
95
- config=generate_content_config,
96
- ):
97
- if chunk.text:
98
- response_text += chunk.text
99
- elif chunk.function_calls:
100
- response_text += chunk.function_calls[0]
 
 
 
 
 
 
 
101
 
102
- return response_text
103
 
104
  @app.get("/", response_class=HTMLResponse)
105
  async def index(request: Request):
106
  return templates.TemplateResponse("index.html", {"request": request})
107
 
 
108
  @app.post("/upload", response_class=JSONResponse)
109
- async def upload_image(file: UploadFile = File(...)):
110
- if not file.content_type.startswith("image/"):
111
- raise HTTPException(status_code=400, detail="Only image files are accepted.")
 
 
 
 
 
112
 
113
- # Save uploaded file temporarily
114
- temp_dir = "temp"
115
- os.makedirs(temp_dir, exist_ok=True)
116
- file_path = os.path.join(temp_dir, file.filename)
117
- with open(file_path, "wb") as buffer:
118
- shutil.copyfileobj(file.file, buffer)
119
 
120
  try:
121
- result = generate_question(file_path)
 
 
 
 
 
 
 
 
 
 
 
122
  finally:
123
- os.remove(file_path)
124
-
125
- return {"result": result}
126
 
127
  if __name__ == "__main__":
128
  import uvicorn
 
1
  import os
2
  import shutil
3
+ import tempfile
4
+ from typing import List
5
+
6
+ from fastapi import FastAPI, File, UploadFile, Form, HTTPException, Request
7
  from fastapi.responses import JSONResponse, HTMLResponse
8
  from fastapi.templating import Jinja2Templates
9
 
10
+ import google.generativeai as genai
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
+ # Configure the API key from environment variables.
13
+ if not os.environ.get("GOOGLE_API_KEY"):
14
+ raise Exception("GOOGLE_API_KEY environment variable not set.")
15
+ genai.configure(api_key=os.environ.get("GOOGLE_API_KEY"))
16
 
17
+ # Choose the Gemini model.
18
+ model = genai.GenerativeModel(model_name="gemini-1.5-pro-latest")
19
 
20
+ app = FastAPI()
21
+ templates = Jinja2Templates(directory="templates")
 
 
 
 
 
 
 
 
22
 
 
 
 
 
23
 
24
+ def generate_content(file_paths: List[str], prompt: str) -> str:
25
+ """
26
+ Given a list of image file paths and a prompt, call the Gemini model
27
+ to generate content. The list provided to generate_content will contain
28
+ the image file paths followed by the prompt.
29
+ """
30
+ # Build the contents list: first the file paths, then the prompt.
31
+ contents = file_paths.copy()
32
+ contents.append(prompt)
33
+
34
+ try:
35
+ response = model.generate_content(contents)
36
+ if not response or not response.text:
37
+ raise Exception("No response text received.")
38
+ return response.text
39
+ except Exception as e:
40
+ print("Error generating content:", e)
41
+ raise e
42
 
 
43
 
44
  @app.get("/", response_class=HTMLResponse)
45
  async def index(request: Request):
46
  return templates.TemplateResponse("index.html", {"request": request})
47
 
48
+
49
  @app.post("/upload", response_class=JSONResponse)
50
+ async def upload_images(
51
+ prompt: str = Form(...),
52
+ files: List[UploadFile] = File(...),
53
+ ):
54
+ # Validate that each uploaded file is an image.
55
+ for file in files:
56
+ if not file.content_type.startswith("image/"):
57
+ raise HTTPException(status_code=400, detail="Only image files are accepted.")
58
 
59
+ temp_dir = tempfile.mkdtemp()
60
+ saved_paths = []
 
 
 
 
61
 
62
  try:
63
+ # Save each uploaded image temporarily.
64
+ for file in files:
65
+ file_path = os.path.join(temp_dir, file.filename)
66
+ with open(file_path, "wb") as buffer:
67
+ shutil.copyfileobj(file.file, buffer)
68
+ saved_paths.append(file_path)
69
+
70
+ # Generate the content using the images and prompt.
71
+ result = generate_content(saved_paths, prompt)
72
+ return {"result": result}
73
+ except Exception as e:
74
+ return JSONResponse(status_code=500, content={"detail": f"Error processing the image(s): {str(e)}"})
75
  finally:
76
+ # Clean up temporary files.
77
+ shutil.rmtree(temp_dir, ignore_errors=True)
78
+
79
 
80
  if __name__ == "__main__":
81
  import uvicorn