main
HuangHai 4 months ago
parent 0487bb1e82
commit 0c0d09fa80

@ -602,12 +602,9 @@ async def generate_upload_params(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']}")
#person_id = current_user['person_id']
person_id = "1"
async def web_recognize_content(image_url: str, current_user: dict = Depends(get_current_user)):
logger.info(f"current_user:{current_user['login_name']}")
person_id = current_user['person_id']
try:
async def generate_stream():
# 假设 recognize_content 是一个异步生成器,逐条返回识别结果
@ -647,12 +644,9 @@ async def web_recognize_text(image_url: str, current_user: dict = Depends(get_cu
@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']}")
#person_id = current_user['person_id']
person_id="1"
async def web_recognize_math(image_url: str, current_user: dict = Depends(get_current_user)):
logger.info(f"current_user:{current_user['login_name']}")
person_id = current_user['person_id']
try:
async def generate_stream():
# 假设 recognize_content 是一个异步生成器,逐条返回识别结果

@ -1,6 +1,3 @@
import requests
from bs4 import BeautifulSoup
from WxMini.Utils.TianQiUtil import *

@ -0,0 +1,5 @@
img_url = 'https://hzkc.oss-cn-beijing.aliyuncs.com/Upload/upsUnGrUnDPjsAXrRXR2pwVtKLZMVf1L.jpg'
from WxMini.Utils.ImageUtil import *
print(getImgWidthHeight(img_url))

@ -1,7 +1,6 @@
import time
import requests
from openai import OpenAI, AsyncOpenAI
from WxMini.Milvus.Config.MulvusConfig import MODELSCOPE_ACCESS_TOKEN
from WxMini.Utils.MySQLUtil import save_chat_to_mysql
@ -44,9 +43,12 @@ async def recognize_text(client, pool, person_id, image_url):
print(char, end='')
time.sleep(0.1) # 控制输出速度
# 获取图片宽高
image_width, image_height = getImgWidthHeight(image_url)
# 记录到数据库
try:
await save_chat_to_mysql(pool, person_id, f'{image_url}', full_text, "", 0, 2, 2, 1)
await save_chat_to_mysql(pool, person_id, f'{image_url}', full_text, "", 0, 2, 2, 1, image_width, image_height)
except Exception as e:
print(f"记录到数据库时出错:{e}")
@ -70,12 +72,14 @@ async def recognize_content(client, pool, person_id, image_url):
if char != ' ':
yield char # 流式输出字符
full_text += char # 拼接字符
#print(char, end='')
# print(char, end='')
time.sleep(0.1) # 控制输出速度
# 获取图片宽高
image_width, image_height = getImgWidthHeight(image_url)
# 记录到数据库
try:
await save_chat_to_mysql(pool, person_id, f'{image_url}', full_text, "", 0, 2, 2, 2)
await save_chat_to_mysql(pool, person_id, f'{image_url}', full_text, "", 0, 2, 2, 2, image_width, image_height)
except Exception as e:
print(f"记录到数据库时出错:{e}")
@ -98,7 +102,7 @@ async def recognize_math(pool, person_id, image_url):
completion = await client.chat.completions.create(
model="Qwen/Qwen2.5-VL-32B-Instruct",
#model="Qwen/Qwen2.5-VL-72B-Instruct",
# model="Qwen/Qwen2.5-VL-72B-Instruct",
messages=[
{
"role": "system",
@ -129,9 +133,24 @@ async def recognize_math(pool, person_id, image_url):
full_text += char # 拼接字符
print(char, end='')
time.sleep(0.1) # 控制输出速度
# 获取图片宽高
image_width, image_height = getImgWidthHeight(image_url)
# 记录到数据库
try:
await save_chat_to_mysql(pool, person_id, f'{image_url}', full_text, "", 0, 2, 2, 1)
await save_chat_to_mysql(pool, person_id, f'{image_url}', full_text, "", 0, 2, 2, 1, image_width, image_height)
except Exception as e:
print(f"记录到数据库时出错:{e}")
# 获取图片的宽高
def getImgWidthHeight(img_url):
try:
url = f'{img_url}?x-oss-process=image/info'
response = requests.get(url)
jo = response.json()
width = int(jo['ImageWidth']['value'])
height = int(jo['ImageHeight']['value'])
return width, height
except:
return 182, 182

@ -27,13 +27,14 @@ async def init_mysql_pool():
# 保存聊天记录到 MySQL
async def save_chat_to_mysql(mysql_pool, person_id, prompt, result, audio_url, duration, input_type=1, output_type=1,
input_image_type=0):
input_image_type=0, image_width=0, image_height=0):
async with mysql_pool.acquire() as conn:
await conn.ping() # 重置连接
async with conn.cursor() as cur:
await cur.execute(
"INSERT INTO t_chat_log (person_id, user_input, model_response,audio_url,duration,input_type,output_type,input_image_type,create_time) VALUES (%s, %s, %s, %s, %s, %s, %s,%s,NOW())",
(person_id, prompt, result, audio_url, duration, input_type, output_type, input_image_type)
"INSERT INTO t_chat_log (person_id, user_input, model_response,audio_url,duration,input_type,output_type,input_image_type,image_width,image_height,create_time) VALUES (%s, %s, %s, %s, %s, %s, %s,%s,%s,%s,NOW())",
(person_id, prompt, result, audio_url, duration, input_type, output_type, input_image_type, image_width,
image_height)
)
await conn.commit()
@ -82,7 +83,7 @@ async def get_chat_log_by_session(mysql_pool, person_id, page=1, page_size=10):
# 查询分页数据,按 id 降序排列
await cur.execute(
"SELECT id, person_id, user_input, model_response, audio_url, duration,input_type,output_type,input_image_type, create_time "
"SELECT id, person_id, user_input, model_response, audio_url, duration,input_type,output_type,input_image_type,image_width,image_height, create_time "
"FROM t_chat_log WHERE person_id = %s ORDER BY id DESC LIMIT %s OFFSET %s",
(person_id, page_size, offset)
)
@ -103,6 +104,8 @@ async def get_chat_log_by_session(mysql_pool, person_id, page=1, page_size=10):
"duration": record['duration'],
"input_type": record['input_type'],
"output_type": record['output_type'],
"image_width": record['image_width'],
"image_height": record['image_height'],
"input_image_type": record['input_image_type'],
"create_time": record['create_time'].strftime("%Y-%m-%d %H:%M:%S")
}

Loading…
Cancel
Save