diff --git a/api/psqlserver/model/Food.py b/api/psqlserver/model/Food.py index 8a73586..daf58da 100644 --- a/api/psqlserver/model/Food.py +++ b/api/psqlserver/model/Food.py @@ -7,9 +7,12 @@ from unidecode import unidecode from itertools import permutations import wikipedia -from bardapi import Bard +import g4f import json +g4f.debug.logging = True # enable logging +g4f.check_version = False # Disable automatic version checkinga + class FoodModel(BaseModel): food_id: int food_name: str @@ -257,16 +260,7 @@ def get_daily_sales(self): return str(e) - def wikipedia_summary(self, food_name: str): - token = 'cwhc0nEvP6vBJCnYULFK5k-1qPGVgnt6KmCtEXv7pIZEMrnROoiRXYqwYUopCJMc1ubDEw.' - bard = Bard(token=token) - content = bard.get_answer(f'As a waiter of a restaurant, generate a description of {food_name} ' - 'in a non-scientific way. The description should be easy to understand for normal people. ' - 'The description must be written in Vietnamese with a language that is close to human language ' - 'as possible. Avoid descriptive and robotic descriptions. The description length must be limited ' - 'to 1 paragraph and must not exceed 30 words. Do not add recommendations in the last sentence.')['content'] - - return content + def get_today_income(self): try: diff --git a/api/psqlserver/requirements.txt b/api/psqlserver/requirements.txt index e6b3816..d86ce98 100644 --- a/api/psqlserver/requirements.txt +++ b/api/psqlserver/requirements.txt @@ -8,4 +8,4 @@ google-auth Unidecode wikipedia pytz -bardapi +g4f diff --git a/api/textai_server/app.py b/api/textai_server/app.py new file mode 100644 index 0000000..637b502 --- /dev/null +++ b/api/textai_server/app.py @@ -0,0 +1,40 @@ +# app.py +from fastapi import FastAPI, Depends, Request, Query +from fastapi.middleware.cors import CORSMiddleware +from starlette.staticfiles import StaticFiles +from fastapi.responses import HTMLResponse +import g4f +import uvicorn +app = FastAPI() +app.add_middleware( + CORSMiddleware, + allow_origins=["*"], # Thay đổi thành nguồn của ứng dụng JavaScript của bạn + allow_credentials=True, + allow_methods=["*"], + allow_headers=["*"], +) + +_providers = [ + g4f.Provider.Aichat, + g4f.Provider.ChatBase, + g4f.Provider.Bing, + g4f.Provider.GptGo, + g4f.Provider.You, + g4f.Provider.Yqcloud, +] + + +@app.get("/generate_description/") +async def generate_description(food_name: str = Query(..., title="Food Name")): + t = f'As a waiter of a restaurant, generate a description of {food_name} in a non-scientific way. The description should be easy to understand for normal people. The description must be written in Vietnamese with a language that is close to human language as possible. Avoid descriptive and robotic descriptions. The description length must be limited to 1 paragraph and must not exceed 30 words. Do not add recommendations in the last sentence.' + + response = g4f.ChatCompletion.create( + model=g4f.models.default, + messages=[{"role": "user", "content": t}], + ) + + return {"description": response} + + +if __name__ == '__main__': + uvicorn.run('app:app', port=8123, host='0.0.0.0') diff --git a/api/textai_server/requirements.txt b/api/textai_server/requirements.txt new file mode 100644 index 0000000..cf25920 --- /dev/null +++ b/api/textai_server/requirements.txt @@ -0,0 +1,6 @@ +uvicorn +pathlib2 +requests +fastapi +protobuf==3.20 +g4f diff --git a/src/main/webapp/WEB-INF/jspf/guest/components/foodDetails.jspf b/src/main/webapp/WEB-INF/jspf/guest/components/foodDetails.jspf index 9a4926f..6a40a3e 100644 --- a/src/main/webapp/WEB-INF/jspf/guest/components/foodDetails.jspf +++ b/src/main/webapp/WEB-INF/jspf/guest/components/foodDetails.jspf @@ -48,7 +48,8 @@ -

Mô tả món ăn.

+

Đang cập nhật mô tả...

@@ -154,8 +155,25 @@ modal.find("#food-discount").text("").addClass("d-none"); } - // Set the description of the food - modal.find("#food-description").html(foodDescription); + + + // Fetch the description via the fetch API + fetch(`http://localhost:8123/generate_description/?food_name=` + foodName) + .then((response) => response.json()) + .then((data) => { + if (data.description) { + foodDescription = data.description; + } else { + foodDescription = "Description not available."; + } + modal.find("#food-description").html(foodDescription); + }) + .catch((error) => { + console.error("Error fetching description:", error); + foodDescription = "Description not available."; + modal.find("#food-description").html(foodDescription); + }); + // Disables (but still shows) quantity input group if the food is out of stock/unavailable if (foodStatus === "0") { diff --git a/src/main/webapp/WEB-INF/jspf/hyperfeat/fooddetechbutton.jspf b/src/main/webapp/WEB-INF/jspf/hyperfeat/fooddetechbutton.jspf index 25cb592..2d0e7b2 100644 --- a/src/main/webapp/WEB-INF/jspf/hyperfeat/fooddetechbutton.jspf +++ b/src/main/webapp/WEB-INF/jspf/hyperfeat/fooddetechbutton.jspf @@ -125,11 +125,10 @@ } const data = await response.json(); - console.log("Predicted Dish:", data.predicted_dish); + console.log("Predicted Dish:", data.predicted_dish[0][0]); if (data.predicted_dish) { const searchURL = "http://localhost:8001/search_food_by_name/" + data.predicted_dish[0][0]; - console.log("data.predicted_dish: ", data.predicted_dish) const searchResponse = await fetch(searchURL); @@ -146,6 +145,7 @@ if (foodDetailsModal.classList.contains("show")) { // If it's showing, hide it const closeModalButton = foodDetailsModal.querySelector("[data-bs-dismiss='modal']"); + foodDetailsModal.querySelector("#food-description").innerHTML = 'Đang cập nhật mô tả...'; closeModalButton.click(); }