Spaces:
Sleeping
Sleeping
Toan2004-ai commited on
Commit ·
bd88cc0
1
Parent(s): 76ac40e
add readme config
Browse files
app.py
CHANGED
|
@@ -1,14 +1,13 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import json
|
| 3 |
import os
|
| 4 |
-
from engine import final_process_eme_technical_guided
|
| 5 |
|
| 6 |
def parse_energy_plan(text_input):
|
| 7 |
if not text_input or not text_input.strip():
|
| 8 |
return json.dumps({"error": "Đầu vào không được để trống"}, indent=2)
|
| 9 |
|
| 10 |
try:
|
| 11 |
-
# Giữ lại logic xử lý JSON đầu vào của bạn vì nó rất tốt
|
| 12 |
final_input = text_input
|
| 13 |
try:
|
| 14 |
parsed_json = json.loads(text_input)
|
|
@@ -17,17 +16,14 @@ def parse_energy_plan(text_input):
|
|
| 17 |
except json.JSONDecodeError:
|
| 18 |
pass
|
| 19 |
|
| 20 |
-
# Gọi engine trực tiếp
|
| 21 |
result = final_process_eme_technical_guided(final_input)
|
| 22 |
|
| 23 |
-
# Chuyển đổi kết quả sang dict
|
| 24 |
result_dict = result.model_dump() if hasattr(result, "model_dump") else result
|
| 25 |
return json.dumps(result_dict, indent=2, ensure_ascii=False)
|
| 26 |
|
| 27 |
except Exception as e:
|
| 28 |
return json.dumps({"error": str(e)}, indent=2, ensure_ascii=False)
|
| 29 |
|
| 30 |
-
# Cấu hình giao diện Gradio
|
| 31 |
demo = gr.Interface(
|
| 32 |
fn=parse_energy_plan,
|
| 33 |
inputs=gr.Textbox(
|
|
@@ -41,5 +37,4 @@ demo = gr.Interface(
|
|
| 41 |
)
|
| 42 |
|
| 43 |
if __name__ == "__main__":
|
| 44 |
-
# Gradio thường dùng cổng 7860 mặc định
|
| 45 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import json
|
| 3 |
import os
|
| 4 |
+
from engine import final_process_eme_technical_guided
|
| 5 |
|
| 6 |
def parse_energy_plan(text_input):
|
| 7 |
if not text_input or not text_input.strip():
|
| 8 |
return json.dumps({"error": "Đầu vào không được để trống"}, indent=2)
|
| 9 |
|
| 10 |
try:
|
|
|
|
| 11 |
final_input = text_input
|
| 12 |
try:
|
| 13 |
parsed_json = json.loads(text_input)
|
|
|
|
| 16 |
except json.JSONDecodeError:
|
| 17 |
pass
|
| 18 |
|
|
|
|
| 19 |
result = final_process_eme_technical_guided(final_input)
|
| 20 |
|
|
|
|
| 21 |
result_dict = result.model_dump() if hasattr(result, "model_dump") else result
|
| 22 |
return json.dumps(result_dict, indent=2, ensure_ascii=False)
|
| 23 |
|
| 24 |
except Exception as e:
|
| 25 |
return json.dumps({"error": str(e)}, indent=2, ensure_ascii=False)
|
| 26 |
|
|
|
|
| 27 |
demo = gr.Interface(
|
| 28 |
fn=parse_energy_plan,
|
| 29 |
inputs=gr.Textbox(
|
|
|
|
| 37 |
)
|
| 38 |
|
| 39 |
if __name__ == "__main__":
|
|
|
|
| 40 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
engine.py
CHANGED
|
@@ -4,7 +4,7 @@ from pydantic.v1 import ValidationError
|
|
| 4 |
import torch
|
| 5 |
from json_repair import repair_json
|
| 6 |
from schema import EnergyTargetSchema
|
| 7 |
-
from
|
| 8 |
|
| 9 |
def final_process_eme_technical_guided(input_text):
|
| 10 |
curr_model, curr_tokenizer = get_model_and_tokenizer()
|
|
|
|
| 4 |
import torch
|
| 5 |
from json_repair import repair_json
|
| 6 |
from schema import EnergyTargetSchema
|
| 7 |
+
from model_manager import get_model_and_tokenizer
|
| 8 |
|
| 9 |
def final_process_eme_technical_guided(input_text):
|
| 10 |
curr_model, curr_tokenizer = get_model_and_tokenizer()
|
main.py
CHANGED
|
@@ -16,11 +16,11 @@ torch.set_num_threads(4)
|
|
| 16 |
async def lifespan(app: FastAPI):
|
| 17 |
logging.basicConfig(level=logging.INFO)
|
| 18 |
logger = logging.getLogger(__name__)
|
| 19 |
-
logger.info("--- [LIFESPAN]
|
| 20 |
get_model_and_tokenizer()
|
| 21 |
yield
|
| 22 |
# Cleanup khi tắt server (nếu cần)
|
| 23 |
-
logger.info("--- [LIFESPAN]
|
| 24 |
|
| 25 |
# Cập nhật khi khai báo app
|
| 26 |
app = FastAPI(title="Volta Energy Plan Parser API", lifespan=lifespan)
|
|
|
|
| 16 |
async def lifespan(app: FastAPI):
|
| 17 |
logging.basicConfig(level=logging.INFO)
|
| 18 |
logger = logging.getLogger(__name__)
|
| 19 |
+
logger.info("--- [LIFESPAN] Starting to load the model into RAM... ---")
|
| 20 |
get_model_and_tokenizer()
|
| 21 |
yield
|
| 22 |
# Cleanup khi tắt server (nếu cần)
|
| 23 |
+
logger.info("--- [LIFESPAN] Stop server. ---")
|
| 24 |
|
| 25 |
# Cập nhật khi khai báo app
|
| 26 |
app = FastAPI(title="Volta Energy Plan Parser API", lifespan=lifespan)
|