Spaces:
Sleeping
Sleeping
Update app.py
#16
by
lucksadasd
- opened
app.py
CHANGED
|
@@ -26,4 +26,34 @@ try:
|
|
| 26 |
print(f"✅ 模型 2 (问答: {model2_name}) 加载成功!")
|
| 27 |
except Exception as e:
|
| 28 |
print(f"❌ 模型 2 (问答: {model2_name}) 加载失败: {e}")
|
| 29 |
-
qa_model = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
print(f"✅ 模型 2 (问答: {model2_name}) 加载成功!")
|
| 27 |
except Exception as e:
|
| 28 |
print(f"❌ 模型 2 (问答: {model2_name}) 加载失败: {e}")
|
| 29 |
+
qa_model = None
|
| 30 |
+
# --- 2. 推理函数 ---
|
| 31 |
+
# 这个函数现在接受一个问题/提示词和一个上下文
|
| 32 |
+
def get_model_outputs(question_or_prompt, context, max_length=100):
|
| 33 |
+
output_text_gen = "文本生成模型未加载或生成失败。"
|
| 34 |
+
output_qa = "问答模型未加载或生成失败。"
|
| 35 |
+
|
| 36 |
+
# 模型 1: 文本生成
|
| 37 |
+
if generator1:
|
| 38 |
+
try:
|
| 39 |
+
# 文本生成模型将问题和上下文作为其prompt的一部分
|
| 40 |
+
full_prompt_for_gen = f"{question_or_prompt}\nContext: {context}" if context else question_or_prompt
|
| 41 |
+
gen_result = generator1(full_prompt_for_gen, max_new_tokens=max_length, num_return_sequences=1, truncation=True)
|
| 42 |
+
output_text_gen = gen_result[0]['generated_text']
|
| 43 |
+
# 清理:移除输入部分,只保留生成内容
|
| 44 |
+
if output_text_gen.startswith(full_prompt_for_gen):
|
| 45 |
+
output_text_gen = output_text_gen[len(full_prompt_for_gen):].strip()
|
| 46 |
+
except Exception as e:
|
| 47 |
+
output_text_gen = f"文本生成模型 ({model1_name}) 错误: {e}"
|
| 48 |
+
|
| 49 |
+
# 模型 2: 问答
|
| 50 |
+
if qa_model and context: # 问答模型必须有上下文
|
| 51 |
+
try:
|
| 52 |
+
qa_result = qa_model(question=question_or_prompt, context=context)
|
| 53 |
+
output_qa = qa_result['answer']
|
| 54 |
+
except Exception as e:
|
| 55 |
+
output_qa = f"问答模型 ({model2_name}) 错误: {e}"
|
| 56 |
+
elif qa_model and not context:
|
| 57 |
+
output_qa = "问答模型需要提供上下文才能回答问题。"
|
| 58 |
+
|
| 59 |
+
return output_text_gen, output_qa
|