Commit
·
3479dfb
1
Parent(s):
c6efb71
add the fine tuned BERT model with FAST API integrated in the Flask app
Browse files- Dockerfile +1 -1
- model/api/__pycache__/api.cpython-39.pyc +0 -0
- model/api/api.py +5 -12
- model/api/start_server.py +0 -1
- model/api/test.py +1 -2
- src/{main.py → app.py} +1 -1
- src/fastapi_server.py +0 -1
- src/templates/index.html +0 -7
Dockerfile
CHANGED
|
@@ -17,4 +17,4 @@ EXPOSE 8000
|
|
| 17 |
|
| 18 |
# Hugging Face Spaces expects the app to run on 0.0.0.0:8000
|
| 19 |
ENV FLASK_APP=src.main
|
| 20 |
-
CMD ["flask", "run", "--host=0.0.0.0", "--port=
|
|
|
|
| 17 |
|
| 18 |
# Hugging Face Spaces expects the app to run on 0.0.0.0:8000
|
| 19 |
ENV FLASK_APP=src.main
|
| 20 |
+
CMD ["flask", "run", "--host=0.0.0.0", "--port=5000", "--no-debugger", "--no-reload"]
|
model/api/__pycache__/api.cpython-39.pyc
ADDED
|
Binary file (1.72 kB). View file
|
|
|
model/api/api.py
CHANGED
|
@@ -13,7 +13,7 @@ BASE_DIR = os.path.dirname(BASE_DIR)
|
|
| 13 |
MODEL_DIR = os.path.join(BASE_DIR, "intent_classifier_model")
|
| 14 |
TOKENIZER_DIR = os.path.join(BASE_DIR, "intent_classifier_tokenizer")
|
| 15 |
|
| 16 |
-
# Ensure model and tokenizer directories exist
|
| 17 |
if not os.path.isdir(MODEL_DIR):
|
| 18 |
raise FileNotFoundError(f"Model directory not found: {MODEL_DIR}")
|
| 19 |
if not os.path.isdir(TOKENIZER_DIR):
|
|
@@ -23,17 +23,10 @@ if not os.path.isdir(TOKENIZER_DIR):
|
|
| 23 |
model = BertForSequenceClassification.from_pretrained(MODEL_DIR, local_files_only=True)
|
| 24 |
tokenizer = BertTokenizer.from_pretrained(TOKENIZER_DIR, local_files_only=True)
|
| 25 |
|
| 26 |
-
#
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
"whisper_mode", "what_are_your_hobbies", "order", "jump_start", "schedule_meeting", "meeting_schedule",
|
| 31 |
-
"freeze_account", "what_song", "meaning_of_life", "restaurant_reservation", "traffic", "pay_bill", "transit_delay",
|
| 32 |
-
"card_declined", "balance", "tell_joke", "last_maintenance", "exchange_rate", "uber", "car_rental", "credit_limit",
|
| 33 |
-
"oos", # ... continue for all 151 intents ...
|
| 34 |
-
]
|
| 35 |
-
def int2str(idx):
|
| 36 |
-
return INTENT_LABELS[idx] if 0 <= idx < len(INTENT_LABELS) else "unknown"
|
| 37 |
|
| 38 |
class Query(BaseModel):
|
| 39 |
text: str
|
|
|
|
| 13 |
MODEL_DIR = os.path.join(BASE_DIR, "intent_classifier_model")
|
| 14 |
TOKENIZER_DIR = os.path.join(BASE_DIR, "intent_classifier_tokenizer")
|
| 15 |
|
| 16 |
+
# Ensure model and tokenizer directories exist
|
| 17 |
if not os.path.isdir(MODEL_DIR):
|
| 18 |
raise FileNotFoundError(f"Model directory not found: {MODEL_DIR}")
|
| 19 |
if not os.path.isdir(TOKENIZER_DIR):
|
|
|
|
| 23 |
model = BertForSequenceClassification.from_pretrained(MODEL_DIR, local_files_only=True)
|
| 24 |
tokenizer = BertTokenizer.from_pretrained(TOKENIZER_DIR, local_files_only=True)
|
| 25 |
|
| 26 |
+
# Load intent label mapping
|
| 27 |
+
from datasets import load_dataset
|
| 28 |
+
dataset = load_dataset("clinc_oos", "small")
|
| 29 |
+
int2str = dataset["train"].features["intent"].int2str
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
class Query(BaseModel):
|
| 32 |
text: str
|
model/api/start_server.py
CHANGED
|
@@ -2,4 +2,3 @@ import uvicorn
|
|
| 2 |
|
| 3 |
if __name__ == "__main__":
|
| 4 |
uvicorn.run("api:app", host="0.0.0.0", port=8000, reload=True)
|
| 5 |
-
|
|
|
|
| 2 |
|
| 3 |
if __name__ == "__main__":
|
| 4 |
uvicorn.run("api:app", host="0.0.0.0", port=8000, reload=True)
|
|
|
model/api/test.py
CHANGED
|
@@ -1,9 +1,8 @@
|
|
| 1 |
import requests
|
| 2 |
|
| 3 |
url = "http://localhost:8000/predict"
|
| 4 |
-
data = {"text": "I
|
| 5 |
|
| 6 |
response = requests.post(url, json=data)
|
| 7 |
print("Status code:", response.status_code)
|
| 8 |
print("Response:", response.json())
|
| 9 |
-
|
|
|
|
| 1 |
import requests
|
| 2 |
|
| 3 |
url = "http://localhost:8000/predict"
|
| 4 |
+
data = {"text": "I need to add 100$ to my bank account."}
|
| 5 |
|
| 6 |
response = requests.post(url, json=data)
|
| 7 |
print("Status code:", response.status_code)
|
| 8 |
print("Response:", response.json())
|
|
|
src/{main.py → app.py}
RENAMED
|
@@ -31,4 +31,4 @@ def index():
|
|
| 31 |
return render_template("index.html", prediction=prediction, user_text=user_text)
|
| 32 |
|
| 33 |
if __name__ == "__main__":
|
| 34 |
-
app.run(
|
|
|
|
| 31 |
return render_template("index.html", prediction=prediction, user_text=user_text)
|
| 32 |
|
| 33 |
if __name__ == "__main__":
|
| 34 |
+
app.run(debug=True)
|
src/fastapi_server.py
CHANGED
|
@@ -15,4 +15,3 @@ def predict(req: PredictRequest):
|
|
| 15 |
else:
|
| 16 |
intent = "unknown"
|
| 17 |
return {"intent": intent}
|
| 18 |
-
|
|
|
|
| 15 |
else:
|
| 16 |
intent = "unknown"
|
| 17 |
return {"intent": intent}
|
|
|
src/templates/index.html
CHANGED
|
@@ -24,12 +24,6 @@
|
|
| 24 |
color: #2d6cdf;
|
| 25 |
margin-bottom: 18px;
|
| 26 |
}
|
| 27 |
-
h2 {
|
| 28 |
-
text-align: center;
|
| 29 |
-
color: #2d6cdf;
|
| 30 |
-
margin-bottom: 18px;
|
| 31 |
-
font-size: 1.5em;
|
| 32 |
-
}
|
| 33 |
label {
|
| 34 |
font-weight: 500;
|
| 35 |
margin-bottom: 8px;
|
|
@@ -97,7 +91,6 @@
|
|
| 97 |
<body>
|
| 98 |
<div class="container">
|
| 99 |
<h1>Intent Classifier Chatbot</h1>
|
| 100 |
-
<h2>Predict User Intent</h2>
|
| 101 |
<div class="info">
|
| 102 |
Enter a message below and click <b>Predict Intent</b> to see what the AI thinks your intent is.<br>
|
| 103 |
<span style="color:#2d6cdf;">Try: <i>"Set an alarm for 7am"</i> or <i>"Transfer money to John"</i></span>
|
|
|
|
| 24 |
color: #2d6cdf;
|
| 25 |
margin-bottom: 18px;
|
| 26 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
label {
|
| 28 |
font-weight: 500;
|
| 29 |
margin-bottom: 8px;
|
|
|
|
| 91 |
<body>
|
| 92 |
<div class="container">
|
| 93 |
<h1>Intent Classifier Chatbot</h1>
|
|
|
|
| 94 |
<div class="info">
|
| 95 |
Enter a message below and click <b>Predict Intent</b> to see what the AI thinks your intent is.<br>
|
| 96 |
<span style="color:#2d6cdf;">Try: <i>"Set an alarm for 7am"</i> or <i>"Transfer money to John"</i></span>
|