DeMaking commited on
Commit
e5d152a
·
verified ·
1 Parent(s): edad6c4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -14
app.py CHANGED
@@ -1,9 +1,11 @@
1
  import os
 
2
  from flask import Flask, request, jsonify
3
  from transformers import pipeline
4
  from langdetect import detect
5
  from huggingface_hub import login
6
 
 
7
  app = Flask(__name__)
8
 
9
  # Gets the Token from secrect
@@ -12,28 +14,63 @@ hf_hub_token = os.getenv("HUGGINGFACEHUB_API_TOKEN")
12
  # Logging in
13
  login(token=hf_hub_token)
14
 
15
- # Load models
16
- hebrew_model = pipeline("text-generation", model="onlplab/alephbert-base", is_decoder=True)
17
- english_model = pipeline("text-generation", model="mistralai/Mistral-7B-Instruct-v0.3")
18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  @app.route("/ask", methods=["POST"])
20
  def ask():
21
  data = request.json
22
  user_input = data.get("text", "")
23
 
24
- # Detect language
25
- language = detect(user_input)
26
 
27
- if language == 'he':
28
- model = hebrew_model
29
- elif language == 'en':
30
- model = english_model
31
- else:
32
- print("Decision Making Helper BOT currently supports Hebrew and English Languages")
33
 
34
- # Create an answer from the model
35
- response = model(user_input, max_length=100, do_sample=True)
36
- return jsonify({"response": response[0]['generated_text']})
 
 
37
 
38
  if __name__ == "__main__":
 
39
  app.run(host="0.0.0.0", port=7860)
 
1
  import os
2
+ import logging
3
  from flask import Flask, request, jsonify
4
  from transformers import pipeline
5
  from langdetect import detect
6
  from huggingface_hub import login
7
 
8
+ # Initialize Flask app
9
  app = Flask(__name__)
10
 
11
  # Gets the Token from secrect
 
14
  # Logging in
15
  login(token=hf_hub_token)
16
 
17
+ # Load Hebrew models (classification + text generation)
18
+ hebrew_classifier = pipeline("text-classification", model="onlplab/alephbert-base")
19
+ hebrew_generator = pipeline("text2text-generation", model="google/mt5-small")
20
 
21
+ # Load English models
22
+ english_classifier = pipeline("text-classification", model="mistralai/Mistral-7B-Instruct-v0.3")
23
+ english_generator = pipeline("text-generation", model="mistralai/Mistral-7B-Instruct-v0.3")
24
+
25
+ # Function to detect language
26
+ def detect_language(user_input):
27
+ try:
28
+ lang = detect(user_input)
29
+ if lang == "he":
30
+ return "hebrew"
31
+ elif lang == "en":
32
+ return "english"
33
+ else:
34
+ return "unsupported"
35
+ except:
36
+ return "unsupported"
37
+
38
+
39
+
40
+ # Function to generate response based on language
41
+ def generate_response(text):
42
+ language = detect_language(text)
43
+
44
+ if language == "hebrew":
45
+ classification = hebrew_classifier(text)
46
+ response = hebrew_generator(text, max_length=100)[0]["generated_text"]
47
+ elif language == "english":
48
+ classification = english_classifier(text)
49
+ response = english_generator(text, max_length=100)[0]["generated_text"]
50
+ else:
51
+ response = "❌ Sorry, I only support Hebrew and English."
52
+
53
+ return response
54
+
55
+
56
+ # Flask endpoint for processing text input
57
  @app.route("/ask", methods=["POST"])
58
  def ask():
59
  data = request.json
60
  user_input = data.get("text", "")
61
 
62
+ if not user_input:
63
+ return jsonify({"error": "No text provided"}), 400
64
 
65
+ response = generate_response(user_input)
66
+ return jsonify({"response": response})
 
 
 
 
67
 
68
+
69
+ # Root endpoint
70
+ @app.route("/")
71
+ def home():
72
+ return "Decision Helper Bot API is running!"
73
 
74
  if __name__ == "__main__":
75
+ logging.basicConfig(level=logging.INFO)
76
  app.run(host="0.0.0.0", port=7860)