Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -165,20 +165,31 @@ def extract_information(api_key, prompt, examples_json, input_text, template_nam
|
|
| 165 |
try:
|
| 166 |
os.environ['LANGEXTRACT_API_KEY'] = api_key
|
| 167 |
|
| 168 |
-
|
| 169 |
text_or_documents=input_text,
|
| 170 |
prompt_description=prompt,
|
| 171 |
examples=examples,
|
| 172 |
model_id="gemini-2.5-flash",
|
| 173 |
)
|
| 174 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
output_dict = {
|
| 176 |
-
"source_text": result.
|
| 177 |
-
"extractions": [ext.to_dict() for ext in result.extractions]
|
| 178 |
}
|
| 179 |
|
|
|
|
| 180 |
with tempfile.NamedTemporaryFile(mode='w+', delete=False, suffix='.jsonl', encoding='utf-8') as tmp_file:
|
| 181 |
-
|
|
|
|
|
|
|
|
|
|
| 182 |
download_path = tmp_file.name
|
| 183 |
|
| 184 |
stats = generate_statistics(output_dict)
|
|
@@ -187,6 +198,9 @@ def extract_information(api_key, prompt, examples_json, input_text, template_nam
|
|
| 187 |
return output_dict, download_path, stats, history, gr.update(visible=True)
|
| 188 |
|
| 189 |
except Exception as e:
|
|
|
|
|
|
|
|
|
|
| 190 |
raise gr.Error(f"❌ 提取失败: {str(e)}")
|
| 191 |
|
| 192 |
def load_template(template_name):
|
|
|
|
| 165 |
try:
|
| 166 |
os.environ['LANGEXTRACT_API_KEY'] = api_key
|
| 167 |
|
| 168 |
+
results = lx.extract(
|
| 169 |
text_or_documents=input_text,
|
| 170 |
prompt_description=prompt,
|
| 171 |
examples=examples,
|
| 172 |
model_id="gemini-2.5-flash",
|
| 173 |
)
|
| 174 |
|
| 175 |
+
# langextract 返回的是列表,取第一个结果
|
| 176 |
+
if isinstance(results, list) and len(results) > 0:
|
| 177 |
+
result = results[0]
|
| 178 |
+
else:
|
| 179 |
+
result = results
|
| 180 |
+
|
| 181 |
+
# 构建输出字典
|
| 182 |
output_dict = {
|
| 183 |
+
"source_text": result.text if hasattr(result, 'text') else input_text,
|
| 184 |
+
"extractions": [ext.to_dict() for ext in result.extractions] if hasattr(result, 'extractions') else []
|
| 185 |
}
|
| 186 |
|
| 187 |
+
# 保存文件
|
| 188 |
with tempfile.NamedTemporaryFile(mode='w+', delete=False, suffix='.jsonl', encoding='utf-8') as tmp_file:
|
| 189 |
+
if isinstance(results, list):
|
| 190 |
+
lx.io.save_annotated_documents(results, file_path=tmp_file.name)
|
| 191 |
+
else:
|
| 192 |
+
lx.io.save_annotated_documents([results], file_path=tmp_file.name)
|
| 193 |
download_path = tmp_file.name
|
| 194 |
|
| 195 |
stats = generate_statistics(output_dict)
|
|
|
|
| 198 |
return output_dict, download_path, stats, history, gr.update(visible=True)
|
| 199 |
|
| 200 |
except Exception as e:
|
| 201 |
+
import traceback
|
| 202 |
+
error_detail = traceback.format_exc()
|
| 203 |
+
print(f"详细错误信息:\n{error_detail}")
|
| 204 |
raise gr.Error(f"❌ 提取失败: {str(e)}")
|
| 205 |
|
| 206 |
def load_template(template_name):
|