dat257 commited on
Commit
5bba1c1
·
verified ·
1 Parent(s): 21f11c9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -13
app.py CHANGED
@@ -7,7 +7,6 @@ from huggingface_hub import login
7
  from fastapi import FastAPI, Request
8
  from fastapi.responses import JSONResponse
9
  import uvicorn
10
- import threading
11
 
12
  # Cấu hình logging
13
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
@@ -33,11 +32,6 @@ except Exception as e:
33
  logging.error(f"Failed to load model: {e}")
34
  raise
35
 
36
- # Hàm xử lý cho Gradio và API
37
- def gradio_answer(question, context):
38
- result = qa_pipeline(question=question, context=context)
39
- return result["answer"]
40
-
41
  # Tạo FastAPI app
42
  app = FastAPI()
43
 
@@ -58,7 +52,8 @@ async def api_answer(request: Request):
58
  logging.error(f"API error: {e}")
59
  return JSONResponse({"error": str(e)}, status_code=500)
60
 
61
- # Tạo Gradio Blocks
 
62
  with gr.Blocks() as demo:
63
  gr.Markdown("# AgriBot: Hỏi đáp nông nghiệp")
64
  gr.Markdown("Nhập câu hỏi và ngữ cảnh để nhận câu trả lời về nông nghiệp.")
@@ -70,17 +65,15 @@ with gr.Blocks() as demo:
70
  submit_btn = gr.Button("Gửi")
71
  submit_btn.click(fn=gradio_answer, inputs=[question_input, context_input], outputs=output)
72
 
73
- # Hàm chạy Gradio
74
  def run_gradio():
75
  logging.info("Starting Gradio...")
76
  demo.launch(server_name="0.0.0.0", server_port=7860, share=False, show_error=True, ssr_mode=False)
77
 
78
- # Chạy ứng dụng
79
  if __name__ == "__main__":
80
- # Chạy Gradio trong một thread riêng
81
  gradio_thread = threading.Thread(target=run_gradio)
82
  gradio_thread.start()
83
-
84
- # Chạy FastAPI (Hugging Face Spaces sẽ tự định nghĩa port)
 
85
  logging.info("Starting FastAPI...")
86
- uvicorn.run(app, host="0.0.0.0", port=80) # Port 80 sẽ được Spaces tự động xử lý
 
7
  from fastapi import FastAPI, Request
8
  from fastapi.responses import JSONResponse
9
  import uvicorn
 
10
 
11
  # Cấu hình logging
12
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
 
32
  logging.error(f"Failed to load model: {e}")
33
  raise
34
 
 
 
 
 
 
35
  # Tạo FastAPI app
36
  app = FastAPI()
37
 
 
52
  logging.error(f"API error: {e}")
53
  return JSONResponse({"error": str(e)}, status_code=500)
54
 
55
+ # (Tạm thời comment Gradio để kiểm tra API)
56
+ """
57
  with gr.Blocks() as demo:
58
  gr.Markdown("# AgriBot: Hỏi đáp nông nghiệp")
59
  gr.Markdown("Nhập câu hỏi và ngữ cảnh để nhận câu trả lời về nông nghiệp.")
 
65
  submit_btn = gr.Button("Gửi")
66
  submit_btn.click(fn=gradio_answer, inputs=[question_input, context_input], outputs=output)
67
 
 
68
  def run_gradio():
69
  logging.info("Starting Gradio...")
70
  demo.launch(server_name="0.0.0.0", server_port=7860, share=False, show_error=True, ssr_mode=False)
71
 
 
72
  if __name__ == "__main__":
 
73
  gradio_thread = threading.Thread(target=run_gradio)
74
  gradio_thread.start()
75
+ """
76
+
77
+ if __name__ == "__main__":
78
  logging.info("Starting FastAPI...")
79
+ uvicorn.run(app, host="0.0.0.0", port=80) # Spaces sẽ tự xử lý cổng