AkCh12 commited on
Commit
a12930f
·
verified ·
1 Parent(s): 6a73991

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -32
app.py CHANGED
@@ -21,14 +21,18 @@ tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
21
  print("[INFO] Loading model...")
22
  model = AutoModelForSequenceClassification.from_pretrained(
23
  MODEL_ID,
24
- num_labels=2
 
25
  )
 
26
  model.to(device)
27
  model.eval()
28
 
 
 
29
  print("[INFO] Warming up model...")
30
  with torch.no_grad():
31
- dummy = tokenizer("Warmup text.", return_tensors="pt").to(device)
32
  _ = model(**dummy)
33
 
34
  print("[INFO] Model ready.")
@@ -36,8 +40,7 @@ print("[INFO] Model ready.")
36
  # ================== HELPERS ==================
37
 
38
  def split_sentences(text: str):
39
- sentences = re.split(r'(?<=[.!?])\s+', text)
40
- return [s.strip() for s in sentences if len(s.strip()) > 10]
41
 
42
 
43
  def score_sentences(sentences):
@@ -47,34 +50,33 @@ def score_sentences(sentences):
47
  truncation=True,
48
  max_length=MAX_LENGTH,
49
  return_tensors="pt"
50
- ).to(device)
51
 
52
  with torch.no_grad():
53
  logits = model(**inputs).logits
54
  probs = F.softmax(logits, dim=-1)
55
 
56
- return probs[:, 1].tolist() # claim probability
57
 
58
 
59
  def extract_claims(text: str, threshold: float):
60
  if not text or len(text.strip()) < 20:
61
  return []
62
 
63
- sentences = split_sentences(text)
64
  if not sentences:
65
  return []
66
 
67
  scores = score_sentences(sentences)
68
 
69
- results = []
70
- for sent, score in zip(sentences, scores):
71
- results.append({
72
- "sentence": sent,
73
- "claim_probability": round(score, 4),
74
- "is_claim": score >= threshold
75
- })
76
-
77
- return results
78
 
79
  # ================== FASTAPI ==================
80
 
@@ -100,19 +102,5 @@ def extract_api(payload: AnalyzeRequest):
100
  def home():
101
  return {
102
  "message": "Claim Extraction API running",
103
- "model": MODEL_ID,
104
- "endpoints": {
105
- "POST /extract": {
106
- "body": {
107
- "text": "news article text",
108
- "threshold": "optional float (default 0.6)"
109
- }
110
- }
111
- }
112
- }
113
-
114
- # ================== ENTRYPOINT ==================
115
-
116
- if __name__ == "__main__":
117
- import uvicorn
118
- uvicorn.run(app, host="0.0.0.0", port=7860)
 
21
  print("[INFO] Loading model...")
22
  model = AutoModelForSequenceClassification.from_pretrained(
23
  MODEL_ID,
24
+ num_labels=2,
25
+ torch_dtype=torch.float32
26
  )
27
+
28
  model.to(device)
29
  model.eval()
30
 
31
+ torch.set_num_threads(1)
32
+
33
  print("[INFO] Warming up model...")
34
  with torch.no_grad():
35
+ dummy = tokenizer("Warmup text.", return_tensors="pt")
36
  _ = model(**dummy)
37
 
38
  print("[INFO] Model ready.")
 
40
  # ================== HELPERS ==================
41
 
42
  def split_sentences(text: str):
43
+ return re.split(r'(?<=[.!?])\s+', text)
 
44
 
45
 
46
  def score_sentences(sentences):
 
50
  truncation=True,
51
  max_length=MAX_LENGTH,
52
  return_tensors="pt"
53
+ )
54
 
55
  with torch.no_grad():
56
  logits = model(**inputs).logits
57
  probs = F.softmax(logits, dim=-1)
58
 
59
+ return probs[:, 1].tolist()
60
 
61
 
62
  def extract_claims(text: str, threshold: float):
63
  if not text or len(text.strip()) < 20:
64
  return []
65
 
66
+ sentences = [s.strip() for s in split_sentences(text) if len(s.strip()) > 10]
67
  if not sentences:
68
  return []
69
 
70
  scores = score_sentences(sentences)
71
 
72
+ return [
73
+ {
74
+ "sentence": s,
75
+ "claim_probability": round(sc, 4),
76
+ "is_claim": sc >= threshold
77
+ }
78
+ for s, sc in zip(sentences, scores)
79
+ ]
 
80
 
81
  # ================== FASTAPI ==================
82
 
 
102
  def home():
103
  return {
104
  "message": "Claim Extraction API running",
105
+ "model": MODEL_ID
106
+ }