Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
50 lines (40 loc) · 1.28 KB
/
Copy pathapp.py
File metadata and controls
50 lines (40 loc) · 1.28 KB
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
41
42
43
44
45
46
47
48
49
50
import os
import base64
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
import uvicorn
from io import BytesIO
from models.semantic_analyzer import SemanticAnalyzer
from models.wordcloud_gen import WordCloudGenerator
app = FastAPI()
analyzer = SemanticAnalyzer()
wordcloud_gen = WordCloudGenerator()
class AnalyzeRequest(BaseModel):
text: str
topics: int = 5
max_words: int = 100
style: str = "light"
@app.post("/analyze")
async def analyze_text(request: AnalyzeRequest):
try:
topics, embeddings = analyzer.analyze(request.text, num_topics=request.topics)
wordcloud_image = wordcloud_gen.generate_wordcloud(
topics,
max_words=request.max_words,
style=request.style
)
buffered = BytesIO()
wordcloud_image.save(buffered, format="PNG")
img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
return {
"topics": topics,
"wordcloud": img_str,
"embeddings": embeddings.tolist()
}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.get("/health")
async def health_check():
return {"status": "ok"}
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)