Update app.py
Browse files
app.py
CHANGED
|
@@ -8,6 +8,49 @@ def encode_image(image_path):
|
|
| 8 |
with open(image_path, "rb") as image_file:
|
| 9 |
return base64.b64encode(image_file.read()).decode('utf-8')
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
def predict(message, history):
|
| 12 |
api_key = os.getenv("SILICONFLOW_API_KEY", "")
|
| 13 |
if not api_key:
|
|
@@ -32,6 +75,12 @@ def predict(message, history):
|
|
| 32 |
else:
|
| 33 |
text_content = str(message)
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
# 构建当前请求的 Message Payload
|
| 36 |
current_content = []
|
| 37 |
|
|
@@ -49,15 +98,32 @@ def predict(message, history):
|
|
| 49 |
print(f"图片读取失败: {img_err}")
|
| 50 |
|
| 51 |
# 2. 解析文本说明
|
| 52 |
-
if not text_content:
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
| 54 |
else:
|
| 55 |
-
prompt_text = text_content
|
|
|
|
| 56 |
current_content.append({"type": "text", "text": prompt_text})
|
| 57 |
|
| 58 |
-
# 3.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
messages = [
|
| 60 |
-
{"role": "system", "content":
|
| 61 |
]
|
| 62 |
|
| 63 |
# 仅保留最近 2 轮纯文本历史
|
|
@@ -72,9 +138,9 @@ def predict(message, history):
|
|
| 72 |
# 放入最新一轮包含图片的请求
|
| 73 |
messages.append({"role": "user", "content": current_content})
|
| 74 |
|
| 75 |
-
# 4.
|
| 76 |
response = client.chat.completions.create(
|
| 77 |
-
model="Qwen/Qwen3-VL-32B-Instruct",
|
| 78 |
messages=messages,
|
| 79 |
stream=True,
|
| 80 |
temperature=0.2
|
|
@@ -99,8 +165,8 @@ def predict(message, history):
|
|
| 99 |
# 创建 Gradio 界面
|
| 100 |
demo = gr.ChatInterface(
|
| 101 |
fn=predict,
|
| 102 |
-
title="🌱 AI 植物识别专家",
|
| 103 |
-
description="
|
| 104 |
multimodal=True
|
| 105 |
)
|
| 106 |
|
|
|
|
| 8 |
with open(image_path, "rb") as image_file:
|
| 9 |
return base64.b64encode(image_file.read()).decode('utf-8')
|
| 10 |
|
| 11 |
+
def detect_language(text, history):
|
| 12 |
+
"""
|
| 13 |
+
智能检测用户语言:
|
| 14 |
+
1. 优先从当前提问检测
|
| 15 |
+
2. 若当前无文字,从最近一轮用户历史中推断
|
| 16 |
+
3. 默认返回中文
|
| 17 |
+
"""
|
| 18 |
+
# 1. 检查当前提问
|
| 19 |
+
if text:
|
| 20 |
+
clean_text = str(text).strip()
|
| 21 |
+
if clean_text:
|
| 22 |
+
# 计算中文字符和英文字母比例
|
| 23 |
+
chinese_chars = sum(1 for char in clean_text if '\u4e00' <= char <= '\u9fff')
|
| 24 |
+
alpha_chars = sum(1 for char in clean_text if char.isalpha())
|
| 25 |
+
|
| 26 |
+
if alpha_chars > 0:
|
| 27 |
+
# 中文字符占比 > 30% 判定为中文
|
| 28 |
+
if chinese_chars / alpha_chars > 0.3:
|
| 29 |
+
return "zh"
|
| 30 |
+
else:
|
| 31 |
+
return "en"
|
| 32 |
+
else:
|
| 33 |
+
# 没有字母(纯数字/符号),默认中文
|
| 34 |
+
return "zh"
|
| 35 |
+
|
| 36 |
+
# 2. 当前无文字,从历史中推断
|
| 37 |
+
for msg in reversed(history[-6:]):
|
| 38 |
+
if msg.get("role") == "user":
|
| 39 |
+
content = msg.get("content", "")
|
| 40 |
+
if isinstance(content, dict):
|
| 41 |
+
content = content.get("text", "")
|
| 42 |
+
if content:
|
| 43 |
+
clean_content = str(content).strip()
|
| 44 |
+
chinese_chars = sum(1 for char in clean_content if '\u4e00' <= char <= '\u9fff')
|
| 45 |
+
alpha_chars = sum(1 for char in clean_content if char.isalpha())
|
| 46 |
+
if alpha_chars > 0 and chinese_chars / alpha_chars > 0.3:
|
| 47 |
+
return "zh"
|
| 48 |
+
elif alpha_chars > 0:
|
| 49 |
+
return "en"
|
| 50 |
+
|
| 51 |
+
# 3. 默认中文
|
| 52 |
+
return "zh"
|
| 53 |
+
|
| 54 |
def predict(message, history):
|
| 55 |
api_key = os.getenv("SILICONFLOW_API_KEY", "")
|
| 56 |
if not api_key:
|
|
|
|
| 75 |
else:
|
| 76 |
text_content = str(message)
|
| 77 |
|
| 78 |
+
# 智能检测语言(含历史记忆)
|
| 79 |
+
user_lang = detect_language(text_content, history)
|
| 80 |
+
|
| 81 |
+
# 🔥 调试输出(可删除)
|
| 82 |
+
print(f"检测到语言: {user_lang}, 用户输入: {text_content[:50]}")
|
| 83 |
+
|
| 84 |
# 构建当前请求的 Message Payload
|
| 85 |
current_content = []
|
| 86 |
|
|
|
|
| 98 |
print(f"图片读取失败: {img_err}")
|
| 99 |
|
| 100 |
# 2. 解析文本说明
|
| 101 |
+
if not text_content or not str(text_content).strip():
|
| 102 |
+
if user_lang == "en":
|
| 103 |
+
prompt_text = "Please observe this image carefully and tell me what plant this is and its key features."
|
| 104 |
+
else:
|
| 105 |
+
prompt_text = "请仔细观察这张图片,告诉我这是什么植物?有哪些特征?"
|
| 106 |
else:
|
| 107 |
+
prompt_text = str(text_content).strip()
|
| 108 |
+
|
| 109 |
current_content.append({"type": "text", "text": prompt_text})
|
| 110 |
|
| 111 |
+
# 3. 根据用户语言设置 System Prompt
|
| 112 |
+
if user_lang == "en":
|
| 113 |
+
system_prompt = (
|
| 114 |
+
"You are a professional botanist and nature expert. "
|
| 115 |
+
"Identify the plant in the image and provide its scientific name, "
|
| 116 |
+
"family, genus, and key features in **English**. "
|
| 117 |
+
"Be concise and accurate."
|
| 118 |
+
)
|
| 119 |
+
else:
|
| 120 |
+
system_prompt = (
|
| 121 |
+
"你是一位专业的植物学家和自然科普专家。"
|
| 122 |
+
"请精准识别图片中的植物,并用简洁明了的中文回答其名称、科属及特点。"
|
| 123 |
+
)
|
| 124 |
+
|
| 125 |
messages = [
|
| 126 |
+
{"role": "system", "content": system_prompt}
|
| 127 |
]
|
| 128 |
|
| 129 |
# 仅保留最近 2 轮纯文本历史
|
|
|
|
| 138 |
# 放入最新一轮包含图片的请求
|
| 139 |
messages.append({"role": "user", "content": current_content})
|
| 140 |
|
| 141 |
+
# 4. 调用视觉模型
|
| 142 |
response = client.chat.completions.create(
|
| 143 |
+
model="Qwen/Qwen3-VL-32B-Instruct",
|
| 144 |
messages=messages,
|
| 145 |
stream=True,
|
| 146 |
temperature=0.2
|
|
|
|
| 165 |
# 创建 Gradio 界面
|
| 166 |
demo = gr.ChatInterface(
|
| 167 |
fn=predict,
|
| 168 |
+
title="🌱 AI 植物识别专家 (智能双语)",
|
| 169 |
+
description="🌍 智能识别你的���问语言:中文问→中文答,英文问→英文答。连续对话自动记忆语言偏好!",
|
| 170 |
multimodal=True
|
| 171 |
)
|
| 172 |
|