dat257 commited on
Commit
2cfcd90
·
verified ·
1 Parent(s): f798fc3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -16
app.py CHANGED
@@ -10,18 +10,42 @@ from huggingface_hub import login
10
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
11
 
12
  # Đăng nhập Hugging Face
13
- login(token=os.getenv("HF_TOKEN")) # Lấy token từ biến môi trường
 
 
 
 
 
14
 
15
  app = Flask(__name__)
16
 
17
  # Load mô hình
18
  logging.info("Loading nguyenvulebinh/vi-mrc-base...")
19
- qa_pipeline = pipeline(
20
- "question-answering",
21
- model="nguyenvulebinh/vi-mrc-base",
22
- device=0 if torch.cuda.is_available() else -1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  )
24
 
 
25
  @app.route("/api/answer", methods=["POST"])
26
  def answer():
27
  try:
@@ -39,18 +63,18 @@ def answer():
39
  logging.error(f"API error: {e}")
40
  return jsonify({"error": str(e)}), 500
41
 
42
- # Giao diện Gradio để thử nghiệm
43
- def gradio_answer(question, context):
44
- result = qa_pipeline(question=question, context=context)
45
- return result["answer"]
46
 
47
- iface = gr.Interface(
48
- fn=gradio_answer,
49
- inputs=["text", "text"],
50
- outputs="text",
51
- title="AgriBot: Hỏi đáp nông nghiệp",
52
- description="Nhập câu hỏi và ngữ cảnh để nhận câu trả lời về nông nghiệp."
53
- )
54
 
55
  if __name__ == "__main__":
 
 
56
  app.run(host="0.0.0.0", port=7860)
 
10
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
11
 
12
  # Đăng nhập Hugging Face
13
+ try:
14
+ login(token=os.getenv("HF_TOKEN"))
15
+ logging.info("Logged in to Hugging Face Hub successfully")
16
+ except Exception as e:
17
+ logging.error(f"Failed to login to Hugging Face Hub: {e}")
18
+ raise
19
 
20
  app = Flask(__name__)
21
 
22
  # Load mô hình
23
  logging.info("Loading nguyenvulebinh/vi-mrc-base...")
24
+ try:
25
+ qa_pipeline = pipeline(
26
+ "question-answering",
27
+ model="nguyenvulebinh/vi-mrc-base",
28
+ device=0 if torch.cuda.is_available() else -1
29
+ )
30
+ logging.info("Model loaded successfully")
31
+ except Exception as e:
32
+ logging.error(f"Failed to load model: {e}")
33
+ raise
34
+
35
+ # Giao diện Gradio
36
+ def gradio_answer(question, context):
37
+ result = qa_pipeline(question=question, context=context)
38
+ return result["answer"]
39
+
40
+ iface = gr.Interface(
41
+ fn=gradio_answer,
42
+ inputs=["text", "text"],
43
+ outputs="text",
44
+ title="AgriBot: Hỏi đáp nông nghiệp",
45
+ description="Nhập câu hỏi và ngữ cảnh để nhận câu trả lời về nông nghiệp."
46
  )
47
 
48
+ # Endpoint API
49
  @app.route("/api/answer", methods=["POST"])
50
  def answer():
51
  try:
 
63
  logging.error(f"API error: {e}")
64
  return jsonify({"error": str(e)}), 500
65
 
66
+ # Health check
67
+ @app.route("/", methods=["GET"])
68
+ def health_check():
69
+ return jsonify({"status": "API is running, use /api/answer for API or Gradio interface"}), 200
70
 
71
+ # Phục vụ Gradio ở root
72
+ @app.route("/", methods=["GET", "POST"])
73
+ @app.route("/<path:path>", methods=["GET", "POST"])
74
+ def serve_gradio(path=None):
75
+ return iface.__call__(request)
 
 
76
 
77
  if __name__ == "__main__":
78
+ logging.info("Starting Flask and Gradio app on port 7860...")
79
+ logging.info(f"Registered routes: {app.url_map}")
80
  app.run(host="0.0.0.0", port=7860)