Spaces:
Runtime error
Runtime error
| # pip install pandas matplotlib requests | |
| # pandas:处理表格数据 | |
| # matplotlib:画图 | |
| # requests:调用你自己的 API(就像其他服务会做的那样) | |
| # 启动服务1 | |
| # python -m uvicorn api:app --reload --port 8000 | |
| # 启动服务2 | |
| # python batch_analyze.py | |
| # 启动服务3 | |
| # python generate_report.py | |
| import os | |
| os.environ['HF_ENDPOINT'] = 'https://hf-mirror.com' | |
| import pandas as pd | |
| import requests | |
| import matplotlib.pyplot as plt | |
| import time | |
| # 1. 读取评论数据 | |
| df = pd.read_csv("reviews.csv") | |
| print(f"共加载 {len(df)} 条评论") | |
| # 2. 调用本地 API 进行情感分析 | |
| results = [] | |
| api_url = "http://localhost:8000/analyze" | |
| for idx, row in df.iterrows(): | |
| try: | |
| response = requests.post(api_url, json={"text": row["review"]}) | |
| if response.status_code == 200: | |
| data = response.json() | |
| results.append({ | |
| "id": row["id"], | |
| "review": row["review"], | |
| "sentiment": data["sentiment"], | |
| "confidence": data["confidence"] | |
| }) | |
| else: | |
| print(f"❌ 分析失败 (ID={row['id']}): {response.text}") | |
| results.append({ | |
| "id": row["id"], | |
| "review": row["review"], | |
| "sentiment": "未知", | |
| "confidence": 0.0 | |
| }) | |
| # 避免请求过快(非必需,但友好) | |
| time.sleep(0.1) | |
| except Exception as e: | |
| print(f"⚠️ 请求异常 (ID={row['id']}): {e}") | |
| results.append({ | |
| "id": row["id"], | |
| "review": row["review"], | |
| "sentiment": "错误", | |
| "confidence": 0.0 | |
| }) | |
| # 转为 DataFrame | |
| result_df = pd.DataFrame(results) | |
| result_df.to_csv("analyzed_reviews.csv", index=False, encoding="utf-8-sig") | |
| print("✅ 分析结果已保存到 analyzed_reviews.csv") |