File size: 1,069 Bytes
10dce3c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from fastapi import FastAPI
from pydantic import BaseModel
from typing import List
from core.preprocess import preprocess_text_list
from core.analyzer import analyze_feedback
from utils.counter import aggregate_results
from dotenv import load_dotenv
import json

load_dotenv()

app = FastAPI(title="Batch Sentiment Analyzer")

class FeedbackBatch(BaseModel):
    texts: List[str]

@app.post("/analyze-batch")
def analyze_batch_endpoint(batch: FeedbackBatch):
    cleaned_texts = preprocess_text_list(batch.texts)
    results = []

    for text in cleaned_texts:
        result = analyze_feedback(text)
        if result:
            results.append(result)

    summary = aggregate_results(results)

    # ❌ Do NOT write to disk on Hugging Face (will crash)
    # ✅ Only enable this locally if needed
    # with open("summary_output.json", "w") as f:
    #     json.dump(summary, f, indent=2)

    return summary

# ✅ Use port 7860 for Hugging Face
if __name__ == "__main__":
    import uvicorn
    uvicorn.run("run_batch_analysis:app", host="0.0.0.0", port=7860)