Spaces:
Running
Running
| import cv2 | |
| import base64 | |
| import requests | |
| import json | |
| import sys | |
| import io | |
| # UTF-8 Encoding ΰΆ ΰΆ±ΰ·ΰ·ΰ·ΰΆ»ΰ·ΰΆΊΰΆΊΰ· (ΰΆ±ΰ·ΰΆΰ·ΰΆ±ΰΆΈΰ· xΒ² ΰΆ ΰΆΰ·ΰΆ» Print ΰ·ΰ·ΰΆ―ΰ·ΰΆ―ΰ· Error ΰΆΰΆ±ΰ·ΰ·) | |
| sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8') | |
| def solve_math_problem(image_path, user_prompt=""): | |
| img = cv2.imread(image_path) | |
| if img is None: | |
| print("β Error: Image not found!") | |
| return | |
| # Image Resize Logic | |
| height, width = img.shape[:2] | |
| max_width = 768 | |
| if width > max_width: | |
| ratio = max_width / width | |
| new_height = int(height * ratio) | |
| img = cv2.resize(img, (max_width, new_height), interpolation=cv2.INTER_AREA) | |
| _, buffer = cv2.imencode('.jpg', img) | |
| base64_image = base64.b64encode(buffer).decode('utf-8') | |
| data_url = f"data:image/jpeg;base64,{base64_image}" | |
| if not user_prompt.strip(): | |
| final_instruction = "Solve this accurately." | |
| else: | |
| final_instruction = f"User Request: {user_prompt}" | |
| # π₯ ΰ·ΰ·ΰ·ΰ·ΰ· Prompt ΰΆΰΆ (Formatting Rules) | |
| prompt_text = f"""You are a math tutor on WhatsApp. | |
| CRITICAL FORMATTING RULES (Follow these strictly): | |
| 1. π« NEVER use the caret symbol (^). | |
| - CORRECT: xΒ², yΒ³, 10β΅ | |
| - WRONG: x^2, y^3 | |
| 2. π« NEVER use asterisk (*) for multiplication. | |
| - CORRECT: 3x Γ 2y | |
| - WRONG: 3x * 2y | |
| 3. π« NEVER use standard slash (/) for division steps if confusing. | |
| - Use 'Γ·' or write clearly. | |
| 4. Write algebraic terms naturally (e.g., 3xΒ² + 2x + 5). | |
| LAYOUT: | |
| π― **Answer:** [Final Result in Bold] | |
| ββββββββββββββββββ | |
| π **Steps:** | |
| [Use Emojis πΉ for bullet points. Keep sentences short.] | |
| ββββββββββββββββββ | |
| INSTRUCTION: {final_instruction}""" | |
| url = "https://text.pollinations.ai/" | |
| payload = { | |
| "messages": [ | |
| { | |
| "role": "user", | |
| "content": [ | |
| {"type": "text", "text": prompt_text}, | |
| {"type": "image_url", "image_url": {"url": data_url}} | |
| ] | |
| } | |
| ], | |
| "model": "gemini", | |
| "jsonMode": False | |
| } | |
| try: | |
| response = requests.post(url, json=payload, headers={"Content-Type": "application/json"}) | |
| response.encoding = 'utf-8' | |
| if response.status_code == 200: | |
| print(response.text) | |
| else: | |
| print("β AI Error.") | |
| except Exception as e: | |
| print(f"β Connection Error: {str(e)}") | |
| if __name__ == "__main__": | |
| img_file = sys.argv[1] if len(sys.argv) > 1 else "math.png" | |
| u_prompt = sys.argv[2] if len(sys.argv) > 2 else "" | |
| solve_math_problem(img_file, u_prompt) |