faizulislamfair commited on
Commit
b554ca5
·
1 Parent(s): 8a1317a

add ai analyzer API

Browse files
Files changed (3) hide show
  1. app.py +22 -1
  2. src/services.py +49 -0
  3. src/utils.py +3 -0
app.py CHANGED
@@ -1,7 +1,28 @@
1
- from fastapi import FastAPI
 
 
 
 
 
2
 
3
  app = FastAPI()
4
 
 
 
5
  @app.get("/")
6
  def health_check():
7
  return {"status": "ok"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from fastapi import FastAPI, Depends, HTTPException, status
3
+ from fastapi.security import APIKeyHeader
4
+
5
+ from src.services import summarize_note, assign_tag, analyze_sentiment
6
+
7
 
8
  app = FastAPI()
9
 
10
+
11
+
12
  @app.get("/")
13
  def health_check():
14
  return {"status": "ok"}
15
+
16
+
17
+
18
+ @app.post("/ai/analyze")
19
+ def analyze_note(note_data: dict, api_key: str = Depends(get_api_key)):
20
+ summary = summarize_note(note_data)
21
+ tag = assign_tag(note_data)
22
+ sentiment = analyze_sentiment(note_data)
23
+
24
+ return {
25
+ "summary": summary,
26
+ "tag": tag,
27
+ "sentiment": sentiment,
28
+ }
src/services.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import pipeline
2
+
3
+ from src.utils import preprocess_text
4
+
5
+
6
+ def summarize_note(note_data):
7
+ """Generate a summary of the note using a pre-trained LLM."""
8
+ try:
9
+ summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
10
+ text = f"{note_data['title']}\n{note_data['note']}"
11
+ summary = summarizer(preprocess_text(text), max_length=100, min_length=20, do_sample=False)[0]["summary_text"]
12
+ return summary
13
+ except Exception as e:
14
+ return f"Error summarizing note: {str(e)}"
15
+
16
+
17
+
18
+ def assign_tag(note_data):
19
+ """Assign a single tag using zero-shot classification with clearer labels."""
20
+ try:
21
+ classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
22
+ text = f"{note_data['title']}\n{note_data['note']}"
23
+
24
+ # More explicit candidate labels
25
+ candidate_labels = ["Educational session", "Project meeting", "Personal note"]
26
+
27
+ result = classifier(preprocess_text(text), candidate_labels, multi_label=False)
28
+
29
+ top_label = result["labels"][0]
30
+
31
+ return top_label
32
+ except Exception as e:
33
+ return {"error": str(e)}
34
+
35
+
36
+
37
+ def analyze_sentiment(note_data) -> dict:
38
+ """Analyze sentiment and map to an emoji."""
39
+ try:
40
+ sentiment_analyzer = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
41
+ text = f"{note_data['title']}\n{note_data['note']}"
42
+ sentiment = sentiment_analyzer(preprocess_text(text))[0]
43
+ label, score = sentiment["label"], sentiment["score"]
44
+ return {
45
+ "label": label,
46
+ "score": score,
47
+ }
48
+ except Exception as e:
49
+ return {"error": str(e)}
src/utils.py ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ def preprocess_text(text: str) -> str:
2
+ """Clean and prepare text for processing."""
3
+ return text.strip().replace("\n", " ")