Update app.py
Browse files
app.py
CHANGED
@@ -1,43 +1,48 @@
|
|
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
|
7 |
import tempfile
|
8 |
from gradio_client import Client, handle_file
|
9 |
-
# import subprocess # Not used
|
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-exp-1206"
|
24 |
-
|
25 |
class MathSolver:
|
26 |
-
def __init__(self
|
27 |
-
self.gemini_config = gemini_config
|
28 |
-
genai.configure(api_key=gemini_config.api_key)
|
29 |
plt.switch_backend('Agg') # Non-interactive backend
|
30 |
|
31 |
def query_gemini(self, image_path: str, prompt: str) -> str:
|
32 |
try:
|
33 |
img = Image.open(image_path)
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
)
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
except Exception as e:
|
42 |
logger.error(f"Gemini Error: {str(e)}")
|
43 |
raise
|
@@ -65,8 +70,7 @@ class MathSolver:
|
|
65 |
image_paths = []
|
66 |
for code in code_blocks:
|
67 |
try:
|
68 |
-
code = "import numpy as np\n" + code
|
69 |
-
# Replace single backslashes with double backslashes
|
70 |
code = code.replace("\\", "\\\\")
|
71 |
|
72 |
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmpfile:
|
@@ -85,22 +89,13 @@ class MathSolver:
|
|
85 |
# Application configuration
|
86 |
app = Flask(__name__)
|
87 |
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
"temperature": 1,
|
93 |
-
"max_output_tokens": 8192,
|
94 |
-
},
|
95 |
-
safety_settings=[
|
96 |
-
{"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_NONE"},
|
97 |
-
{"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_NONE"},
|
98 |
-
{"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_NONE"},
|
99 |
-
{"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_NONE"},
|
100 |
-
]
|
101 |
)
|
102 |
|
103 |
-
math_solver = MathSolver(
|
104 |
|
105 |
@app.route('/')
|
106 |
def index():
|
@@ -130,7 +125,6 @@ def upload_image():
|
|
130 |
else math_solver.query_qwen2(temp_file.name, prompt)
|
131 |
)
|
132 |
|
133 |
-
# Extract and generate graphs
|
134 |
image_paths = math_solver.extract_and_execute_python_code(result)
|
135 |
os.unlink(temp_file.name)
|
136 |
|
|
|
1 |
from flask import Flask, request, render_template, jsonify, send_from_directory
|
2 |
from PIL import Image
|
3 |
+
import io
|
4 |
import os
|
5 |
import re
|
6 |
import matplotlib.pyplot as plt
|
7 |
import tempfile
|
8 |
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 |
plt.switch_backend('Agg') # Non-interactive backend
|
22 |
|
23 |
def query_gemini(self, image_path: str, prompt: str) -> str:
|
24 |
try:
|
25 |
img = Image.open(image_path)
|
26 |
+
buffered = io.BytesIO()
|
27 |
+
img.save(buffered, format="PNG")
|
28 |
+
img_byte_arr = buffered.getvalue()
|
29 |
+
|
30 |
+
response = client.models.generate_content(
|
31 |
+
model="gemini-2.0-flash-thinking-exp-01-21",
|
32 |
+
config={'thinking_config': {'include_thoughts': True}, 'temperature': 1, 'max_output_tokens': 8192},
|
33 |
+
contents=[
|
34 |
+
{'parts': [{'text': prompt}, {'inline_data': {'mime_type': 'image/png', 'data': img_byte_arr}}]}
|
35 |
+
]
|
36 |
)
|
37 |
+
|
38 |
+
full_response = ""
|
39 |
+
for candidate in response.candidates:
|
40 |
+
for part in candidate.content.parts:
|
41 |
+
if part.thought:
|
42 |
+
full_response += f"<br><b>Thought:</b><br> {part.text}<br>"
|
43 |
+
else:
|
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
|
|
|
70 |
image_paths = []
|
71 |
for code in code_blocks:
|
72 |
try:
|
73 |
+
code = "import numpy as np\n" + code
|
|
|
74 |
code = code.replace("\\", "\\\\")
|
75 |
|
76 |
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmpfile:
|
|
|
89 |
# Application configuration
|
90 |
app = Flask(__name__)
|
91 |
|
92 |
+
GOOGLE_API_KEY = os.environ.get("TOKEN")
|
93 |
+
client = genai.Client(
|
94 |
+
api_key=GOOGLE_API_KEY,
|
95 |
+
http_options={'api_version': 'v1alpha'},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
96 |
)
|
97 |
|
98 |
+
math_solver = MathSolver()
|
99 |
|
100 |
@app.route('/')
|
101 |
def index():
|
|
|
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 |
|