abeerrai01 commited on
Commit
fc5d042
·
0 Parent(s):
.gitignore ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ .venv
2
+ .idea
Dockerfile ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # ===== Base Image =====
2
+ FROM python:3.10-slim
3
+
4
+ # ===== Set working directory =====
5
+ WORKDIR /app
6
+
7
+ # ===== Copy project files =====
8
+ COPY . /app
9
+
10
+ # ===== Install dependencies =====
11
+ RUN pip install --no-cache-dir -r requirements.txt
12
+
13
+ # ===== Expose the port =====
14
+ EXPOSE 7860
15
+
16
+ # ===== Run FastAPI using uvicorn =====
17
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
__pycache__/app.cpython-39.pyc ADDED
Binary file (1.2 kB). View file
 
__pycache__/suicidality_model.cpython-39.pyc ADDED
Binary file (1.24 kB). View file
 
__pycache__/translator.cpython-39.pyc ADDED
Binary file (511 Bytes). View file
 
app.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import uvicorn
2
+ from fastapi import FastAPI
3
+ from pydantic import BaseModel
4
+ from suicidality_model import predict_suicidality
5
+ from translator import to_english
6
+
7
+ app = FastAPI(title="🌾 Farmer Mental Health AI")
8
+
9
+ # ✅ Define a request model
10
+ class TextInput(BaseModel):
11
+ text: str
12
+
13
+ @app.get("/")
14
+ def home():
15
+ return {"message": "Farmer Mental Health AI is up and running 🚀"}
16
+
17
+ # ✅ Accept JSON body input
18
+ @app.post("/analyze/")
19
+ def analyze_text(data: TextInput):
20
+ text = data.text
21
+ text_en = to_english(text)
22
+ result = predict_suicidality(text_en)
23
+ return {
24
+ "original_text": text,
25
+ "translated_to_english": text_en,
26
+ "label": result["label"],
27
+ "confidence": result["confidence"],
28
+ "sentiment": result["sentiment"]
29
+ }
30
+
31
+ if __name__ == "__main__":
32
+ uvicorn.run("app:app", host="0.0.0.0", port=9000, reload=True)
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ fastapi
2
+ uvicorn
3
+ pydantic
4
+ googletrans==4.0.0-rc1
5
+ transformers
6
+ torch
suicidality_model.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
2
+ from textblob import TextBlob
3
+ import torch
4
+
5
+ # Load the Hugging Face model
6
+ model_name = "sentinet/suicidality"
7
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
8
+ model = AutoModelForSequenceClassification.from_pretrained(model_name)
9
+
10
+ labels = ["non-suicidal", "suicidal"]
11
+
12
+ def sentiment_score(text):
13
+ """Calculate basic sentiment polarity (-1 = negative, +1 = positive)."""
14
+ blob = TextBlob(text)
15
+ return round(blob.sentiment.polarity, 3)
16
+
17
+ def predict_suicidality(text: str):
18
+ """Predict suicidality and sentiment for the given (English) text."""
19
+ inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
20
+ with torch.no_grad():
21
+ logits = model(**inputs).logits
22
+ probs = torch.softmax(logits, dim=1)
23
+ pred_class = torch.argmax(probs, dim=1).item()
24
+ confidence = probs[0][pred_class].item()
25
+
26
+ sentiment = sentiment_score(text) # ✅ this is now correctly scoped
27
+
28
+ return {
29
+ "label": labels[pred_class],
30
+ "confidence": round(confidence, 3),
31
+ "sentiment": sentiment
32
+ }
translator.py ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from googletrans import Translator
2
+
3
+ translator = Translator()
4
+
5
+ def to_english(text):
6
+ result = translator.translate(text, src='auto', dest='en')
7
+ return result.text
8
+
9
+ def to_hindi(text):
10
+ result = translator.translate(text, src='en', dest='hi')
11
+ return result.text