Update app.py
Browse files
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 |
-
#
|
| 36 |
def gradio_answer(question, context):
|
| 37 |
result = qa_pipeline(question=question, context=context)
|
| 38 |
return result["answer"]
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 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 |
-
#
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
|
|
|
| 73 |
|
| 74 |
-
|
| 75 |
-
|
| 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)
|
|
|
|
|
|