dat257 commited on
Commit
26c5e44
·
verified ·
1 Parent(s): 15fd624

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -22
app.py CHANGED
@@ -17,8 +17,6 @@ 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:
@@ -32,20 +30,14 @@ 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,15 +55,14 @@ def answer():
63
  logging.error(f"API error: {e}")
64
  return jsonify({"error": str(e)}), 500
65
 
66
- # Health check (chỉ dùng khi cần debug)
67
- @app.route("/health", methods=["GET"])
68
- def health_check():
69
- return jsonify({"status": "API is running, use /api/answer for API or / for Gradio interface"}), 200
70
-
71
- # Phục vụ Gradio ở root
72
- app = gr.mount_gradio_app(app, iface, path="/")
 
73
 
74
- if __name__ == "__main__":
75
- logging.info("Starting Flask and Gradio app on port 7860...")
76
- logging.info(f"Registered routes: {app.url_map}")
77
- app.run(host="0.0.0.0", port=7860)
 
17
  logging.error(f"Failed to login to Hugging Face Hub: {e}")
18
  raise
19
 
 
 
20
  # Load mô hình
21
  logging.info("Loading nguyenvulebinh/vi-mrc-base...")
22
  try:
 
30
  logging.error(f"Failed to load model: {e}")
31
  raise
32
 
33
+ # Hàm xử lý cho Gradio và API
34
  def gradio_answer(question, context):
35
  result = qa_pipeline(question=question, context=context)
36
  return result["answer"]
37
 
38
+ # Tạo Flask app để thêm endpoint API
39
+ app = Flask(__name__)
 
 
 
 
 
40
 
 
41
  @app.route("/api/answer", methods=["POST"])
42
  def answer():
43
  try:
 
55
  logging.error(f"API error: {e}")
56
  return jsonify({"error": str(e)}), 500
57
 
58
+ # Tạo Gradio interface
59
+ iface = gr.Interface(
60
+ fn=gradio_answer,
61
+ inputs=["text", "text"],
62
+ outputs="text",
63
+ title="AgriBot: Hỏi đáp nông nghiệp",
64
+ description="Nhập câu hỏi và ngữ cảnh để nhận câu trả lời về nông nghiệp."
65
+ )
66
 
67
+ # Thêm Flask app vào Gradio
68
+ iface.launch(server_name="0.0.0.0", server_port=7860, app=app)