Spaces:
Runtime error
Runtime error
import gradio as gr | |
import pytesseract | |
from PIL import Image | |
import requests | |
import re | |
import traceback | |
import os | |
# 配置 Tesseract OCR 的路径(Hugging Face Spaces 自动配置) | |
pytesseract.pytesseract.tesseract_cmd = '/usr/bin/tesseract' | |
# 使用环境变量获取 Hugging Face API Token | |
API_URL = "https://api-inference.huggingface.co/models/Qwen/Qwen2.5-Math-72B-Instruct" | |
API_TOKEN = os.getenv("HF_API_TOKEN") # 从环境变量获取 Token | |
HEADERS = {"Authorization": f"Bearer {API_TOKEN}"} | |
# OCR 识别函数 | |
def ocr_with_tesseract(image_path): | |
try: | |
image = Image.open(image_path).convert("L") | |
config = "--psm 6" | |
text = pytesseract.image_to_string(image, config=config) | |
text = re.sub(r'[^0-9a-zA-Z=+\-*/()., ]', '', text) | |
return text if text else "OCR 识别失败" | |
except Exception as e: | |
return f"OCR 识别错误: {e}\n{traceback.format_exc()}" | |
# AI 解答生成函数 | |
def generate_solution_with_qwen(question): | |
prompt = f"请详细解答以下数学题目:{question}" | |
payload = {"inputs": prompt} | |
response = requests.post(API_URL, headers=HEADERS, json=payload) | |
if response.status_code == 200: | |
result = response.json() | |
return result.get('generated_text', "解答生成失败") | |
else: | |
return f"API 调用失败,状态码: {response.status_code}, 响应: {response.text}" | |
# 主处理函数 | |
def process(image_path): | |
ocr_result = ocr_with_tesseract(image_path) | |
ai_solution = generate_solution_with_qwen(ocr_result) | |
return ocr_result, ai_solution | |
# 构建 Gradio 应用界面 | |
def build_interface(): | |
with gr.Blocks() as interface: | |
gr.Markdown("# 📚 高级 AI 数学解题助手") | |
image_input = gr.Image(type="filepath", label="上传数学题目图片") | |
ocr_output = gr.Textbox(label="OCR 识别结果") | |
ai_output = gr.Markdown(label="AI 解答") | |
submit_button = gr.Button("识别并解答") | |
submit_button.click(fn=process, inputs=image_input, outputs=[ocr_output, ai_output]) | |
return interface | |
# 启动 Gradio 应用 | |
interface = build_interface() | |
interface.launch() | |