PolyakovK commited on
Commit
b4e675c
·
verified ·
1 Parent(s): ff9c858

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -17
app.py CHANGED
@@ -1,22 +1,41 @@
1
- from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
2
- import torch
3
 
4
- # Загрузка модели и токенизатора
5
- model_id = "beyond/roberta-base-email-intent-classifier"
6
- tokenizer = AutoTokenizer.from_pretrained(model_id)
7
- model = AutoModelForSequenceClassification.from_pretrained(model_id)
8
 
9
- # pipeline с device="cpu" (или "cuda:0" если есть GPU)
10
- classifier = pipeline("text-classification", model=model, tokenizer=tokenizer, device=0 if torch.cuda.is_available() else -1)
11
 
12
- # Пример: классификация писем
13
- emails = [
14
- "Спасибо за ваше письмо. Давайте созвонимся в пятницу.",
15
- "К сожалению, мы не готовы сотрудничать в этом направлении.",
16
- "А подскажите, пожалуйста, как вы работаете с B2B и есть ли у вас кейсы в финансовой сфере?"
17
- ]
18
 
19
- results = classifier(emails)
 
 
 
20
 
21
- for email, res in zip(emails, results):
22
- print(f"Email: {email}\n→ Predicted intent: {res['label']} (confidence: {res['score']:.2f})\n")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import pipeline
2
+ from flask import Flask, request, jsonify
3
 
4
+ app = Flask(__name__)
 
 
 
5
 
6
+ # Загружаем zero-shot классификатор
7
+ classifier = pipeline("zero-shot-classification", model="typeform/distilbert-base-uncased-mnli")
8
 
9
+ # Гипотезы
10
+ LABELS = {
11
+ "wants_meeting": "Клиент хочет назначить встречу или обсудить время",
12
+ "not_interested": "Клиент не заинтересован во встрече или у него нет времени",
13
+ "asking_questions": "Клиент задает уточняющие вопросы по теме"
14
+ }
15
 
16
+ @app.route("/analyze", methods=["POST"])
17
+ def analyze():
18
+ data = request.get_json()
19
+ emails = data.get("emails", [])
20
 
21
+ if not emails or not isinstance(emails, list):
22
+ return jsonify({"error": "Field 'emails' must be a non-empty list"}), 400
23
+
24
+ results = []
25
+ for email in emails:
26
+ prediction = classifier(email, list(LABELS.values()), multi_label=True)
27
+ scored_labels = dict(zip(prediction["labels"], prediction["scores"]))
28
+
29
+ # Вывод в формате {label: score}
30
+ result = {
31
+ "text": email,
32
+ "intents": {
33
+ key: round(scored_labels[label], 4) for key, label in LABELS.items()
34
+ }
35
+ }
36
+ results.append(result)
37
+
38
+ return jsonify(results)
39
+
40
+ if __name__ == "__main__":
41
+ app.run(host="0.0.0.0", port=7860)