Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
from flask import Flask, request, render_template, jsonify, send_from_directory
|
2 |
from PIL import Image
|
3 |
-
import
|
|
|
4 |
import os
|
5 |
import re
|
6 |
import matplotlib.pyplot as plt
|
@@ -9,40 +10,46 @@ from gradio_client import Client, handle_file
|
|
9 |
from dataclasses import dataclass
|
10 |
from typing import List, Optional
|
11 |
import logging
|
12 |
-
from google import genai
|
13 |
-
from google.genai import types
|
14 |
|
15 |
# Logging configuration
|
16 |
logging.basicConfig(level=logging.INFO)
|
17 |
logger = logging.getLogger(__name__)
|
18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
class MathSolver:
|
20 |
-
def __init__(self):
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
def query_gemini(self, image_path: str, prompt: str) -> str:
|
24 |
try:
|
25 |
img = Image.open(image_path)
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
contents=[
|
34 |
-
{'parts': [{'text': prompt}, {'inline_data': {'mime_type': 'image/png', 'data': img_byte_arr}}]}
|
35 |
-
]
|
36 |
)
|
37 |
-
|
38 |
-
|
39 |
-
for
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
full_response += f"<br><b>Answer:</b><br> {part.text}<br>"
|
45 |
-
return full_response
|
46 |
except Exception as e:
|
47 |
logger.error(f"Gemini Error: {str(e)}")
|
48 |
raise
|
@@ -89,13 +96,22 @@ class MathSolver:
|
|
89 |
# Application configuration
|
90 |
app = Flask(__name__)
|
91 |
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
96 |
)
|
97 |
|
98 |
-
math_solver = MathSolver()
|
99 |
|
100 |
@app.route('/')
|
101 |
def index():
|
@@ -121,10 +137,11 @@ def upload_image():
|
|
121 |
|
122 |
result = (
|
123 |
math_solver.query_gemini(temp_file.name, prompt)
|
124 |
-
if model_choice == "
|
125 |
else math_solver.query_qwen2(temp_file.name, prompt)
|
126 |
)
|
127 |
|
|
|
128 |
image_paths = math_solver.extract_and_execute_python_code(result)
|
129 |
os.unlink(temp_file.name)
|
130 |
|
|
|
1 |
from flask import Flask, request, render_template, jsonify, send_from_directory
|
2 |
from PIL import Image
|
3 |
+
import google.genai as genai
|
4 |
+
from google.genai import types
|
5 |
import os
|
6 |
import re
|
7 |
import matplotlib.pyplot as plt
|
|
|
10 |
from dataclasses import dataclass
|
11 |
from typing import List, Optional
|
12 |
import logging
|
|
|
|
|
13 |
|
14 |
# Logging configuration
|
15 |
logging.basicConfig(level=logging.INFO)
|
16 |
logger = logging.getLogger(__name__)
|
17 |
|
18 |
+
@dataclass
|
19 |
+
class GeminiConfig:
|
20 |
+
api_key: str
|
21 |
+
generation_config: dict
|
22 |
+
safety_settings: List[dict]
|
23 |
+
model_name: str = "gemini-2.0-flash-thinking-exp-01-21"
|
24 |
+
|
25 |
class MathSolver:
|
26 |
+
def __init__(self, gemini_config: GeminiConfig):
|
27 |
+
self.gemini_config = gemini_config
|
28 |
+
self.client = genai.Client(
|
29 |
+
api_key=gemini_config.api_key,
|
30 |
+
http_options={'api_version': 'v1alpha'}
|
31 |
+
)
|
32 |
+
plt.switch_backend('Agg')
|
33 |
|
34 |
def query_gemini(self, image_path: str, prompt: str) -> str:
|
35 |
try:
|
36 |
img = Image.open(image_path)
|
37 |
+
response = self.client.models.generate_content(
|
38 |
+
model=self.gemini_config.model_name,
|
39 |
+
config={
|
40 |
+
'thinking_config': {'include_thoughts': True},
|
41 |
+
'generation_config': self.gemini_config.generation_config,
|
42 |
+
'safety_settings': self.gemini_config.safety_settings
|
43 |
+
},
|
44 |
+
contents=[prompt, img]
|
|
|
|
|
45 |
)
|
46 |
+
|
47 |
+
result = ""
|
48 |
+
for part in response.candidates[0].content.parts:
|
49 |
+
if not part.thought:
|
50 |
+
result += part.text + "\n"
|
51 |
+
return result.strip()
|
52 |
+
|
|
|
|
|
53 |
except Exception as e:
|
54 |
logger.error(f"Gemini Error: {str(e)}")
|
55 |
raise
|
|
|
96 |
# Application configuration
|
97 |
app = Flask(__name__)
|
98 |
|
99 |
+
token = os.environ.get("TOKEN")
|
100 |
+
gemini_config = GeminiConfig(
|
101 |
+
token, # Replace with your actual API key
|
102 |
+
generation_config={
|
103 |
+
"temperature": 1,
|
104 |
+
"max_output_tokens": 8192,
|
105 |
+
},
|
106 |
+
safety_settings=[
|
107 |
+
{"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_NONE"},
|
108 |
+
{"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_NONE"},
|
109 |
+
{"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_NONE"},
|
110 |
+
{"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_NONE"},
|
111 |
+
]
|
112 |
)
|
113 |
|
114 |
+
math_solver = MathSolver(gemini_config)
|
115 |
|
116 |
@app.route('/')
|
117 |
def index():
|
|
|
137 |
|
138 |
result = (
|
139 |
math_solver.query_gemini(temp_file.name, prompt)
|
140 |
+
if model_choice == "gemini"
|
141 |
else math_solver.query_qwen2(temp_file.name, prompt)
|
142 |
)
|
143 |
|
144 |
+
# Extract and generate graphs
|
145 |
image_paths = math_solver.extract_and_execute_python_code(result)
|
146 |
os.unlink(temp_file.name)
|
147 |
|