main
HuangHai 4 months ago
parent 60387b6821
commit 6ba01b0c03

@ -595,16 +595,10 @@ async def generate_upload_params(current_user: dict = Depends(get_current_user))
return response_data
@app.get("/aichat/process_image")
async def process_image(image_url: str, current_user: dict = Depends(get_current_user)):
@app.get("/aichat/recognize_content")
async def web_recognize_content(image_url: str, current_user: dict = Depends(get_current_user)):
logger.info(f"current_user:{current_user['login_name']}")
"""
处理图片自动判断调用哪个功能
:param image_url: 图片 URL
:return: 流式输出结果
"""
try:
print("检测到图片主要是物体/场景,开始识别内容:")
return StreamingResponse(
recognize_content(client, app.state.mysql_pool, current_user['person_id'], image_url),
media_type="text/plain")
@ -612,6 +606,28 @@ async def process_image(image_url: str, current_user: dict = Depends(get_current
raise HTTPException(status_code=500, detail=str(e))
@app.get("/aichat/recognize_text")
async def web_recognize_text(image_url: str, current_user: dict = Depends(get_current_user)):
logger.info(f"current_user:{current_user['login_name']}")
try:
return StreamingResponse(
recognize_text(client, app.state.mysql_pool, current_user['person_id'], image_url),
media_type="text/plain")
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.get("/aichat/recognize_math")
async def web_recognize_math(image_url: str, current_user: dict = Depends(get_current_user)):
logger.info(f"current_user:{current_user['login_name']}")
try:
return StreamingResponse(
recognize_math(app.state.mysql_pool, current_user['person_id'], image_url),
media_type="text/plain")
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
# 运行 FastAPI 应用
if __name__ == "__main__":
import uvicorn

@ -1,7 +1,11 @@
import time
from openai import OpenAI, AsyncOpenAI
from WxMini.Milvus.Config.MulvusConfig import MODELSCOPE_ACCESS_TOKEN
from WxMini.Utils.MySQLUtil import save_chat_to_mysql
async def recognize_text(client, pool, person_id, image_url):
"""
识别图片中的文字流式输出并将结果记录到数据库
@ -37,6 +41,7 @@ async def recognize_text(client, pool, person_id, image_url):
if char != ' ':
yield char # 流式输出字符
full_text += char # 拼接字符
print(char, end='')
time.sleep(0.1) # 控制输出速度
# 记录到数据库
@ -65,6 +70,7 @@ async def recognize_content(client, pool, person_id, image_url):
if char != ' ':
yield char # 流式输出字符
full_text += char # 拼接字符
print(char, end='')
time.sleep(0.1) # 控制输出速度
# 记录到数据库
@ -72,3 +78,59 @@ async def recognize_content(client, pool, person_id, image_url):
await save_chat_to_mysql(pool, person_id, f'{image_url}', full_text, "", 0, 2, 2, 2)
except Exception as e:
print(f"记录到数据库时出错:{e}")
async def recognize_math(pool, person_id, image_url):
client = AsyncOpenAI(
api_key=MODELSCOPE_ACCESS_TOKEN,
base_url="https://api-inference.modelscope.cn/v1"
)
"""
识别图片中的数学题流式输出并将结果记录到数据库
:param client: AsyncOpenAI 客户端
:param pool: 数据库连接池
:param person_id: 用户 ID
:param image_url: 图片 URL
:return: 最终拼接的字符串
"""
# 提示词
prompt = "You are a helpful and harmless assistant. You are Qwen developed by Alibaba. You should think step-by-step."
completion = await client.chat.completions.create(
model="Qwen/Qwen2.5-VL-32B-Instruct",
messages=[
{
"role": "system",
"content": [
{"type": "text", "text": prompt}
],
},
{
"role": "user",
"content": [
{
"type": "image_url",
"image_url": {"url": image_url}
},
{"type": "text", "text": "请使用中文回答:如何作答?"},
],
}
],
stream=True
)
full_text = "" # 用于存储最终拼接的字符串
async for chunk in completion:
if chunk.choices[0].delta.content is not None:
for char in chunk.choices[0].delta.content:
if char != ' ':
yield char # 流式输出字符
full_text += char # 拼接字符
print(char, end='')
time.sleep(0.1) # 控制输出速度
# 记录到数据库
try:
await save_chat_to_mysql(pool, person_id, f'{image_url}', full_text, "", 0, 2, 2, 1)
except Exception as e:
print(f"记录到数据库时出错:{e}")

Loading…
Cancel
Save