From c12bf86c94f9c132f71d38a2f572eb4443a7cfa7 Mon Sep 17 00:00:00 2001 From: khaangnguyeen <78076796+khengyun@users.noreply.github.com> Date: Tue, 7 Nov 2023 17:39:23 +0700 Subject: [PATCH 1/3] udpate textai server --- api/psqlserver/model/Food.py | 49 +++++++++++++++++++----------- api/psqlserver/requirements.txt | 4 +-- api/textai_server/app.py | 40 ++++++++++++++++++++++++ api/textai_server/requirements.txt | 6 ++++ 4 files changed, 79 insertions(+), 20 deletions(-) create mode 100644 api/textai_server/app.py create mode 100644 api/textai_server/requirements.txt diff --git a/api/psqlserver/model/Food.py b/api/psqlserver/model/Food.py index de663e4d..71312b82 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 @@ -104,13 +107,6 @@ def search_food_by_name(self, food_name: str): for record in records: - # add if record[2] is null call wikipedia_summary - print("record[2]: ",record[2]) - print("record[2] type: ",type(record[2])) - # content = record[2] - # if content == None: - # content = self.wikipedia_summary(record[1]) - # print("content: ",content) food = FoodModel( @@ -258,13 +254,30 @@ 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 \ No newline at end of file + + + def get_today_income(self): + try: + with pymssql.connect(**self.db_config) as conn: + cursor = conn.cursor() + + daily_payments = [] + + cursor.execute(""" + SELECT SUM(payment_total) AS total_payment_today + FROM Payment + WHERE + DAY(payment_time) = DAY(SYSDATETIME()) + AND MONTH(payment_time) = MONTH(SYSDATETIME()) + AND YEAR(payment_time) = YEAR(SYSDATETIME()); + """) + + row = cursor.fetchone() + today_income = float(row[0]) if row and row[0] is not None else 0.0 + + daily_payments.append({"today_income": today_income}) + + return daily_payments + + except Exception as e: + return str(e) diff --git a/api/psqlserver/requirements.txt b/api/psqlserver/requirements.txt index c788bc57..d86ce988 100644 --- a/api/psqlserver/requirements.txt +++ b/api/psqlserver/requirements.txt @@ -7,5 +7,5 @@ pymssql google-auth Unidecode wikipedia -bardapi -pytz \ No newline at end of file +pytz +g4f diff --git a/api/textai_server/app.py b/api/textai_server/app.py new file mode 100644 index 00000000..637b502f --- /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 00000000..cf259209 --- /dev/null +++ b/api/textai_server/requirements.txt @@ -0,0 +1,6 @@ +uvicorn +pathlib2 +requests +fastapi +protobuf==3.20 +g4f From 1c242c8fdb6cde8b7d991af11baad0ae9f9a2e13 Mon Sep 17 00:00:00 2001 From: khaangnguyeen <78076796+khengyun@users.noreply.github.com> Date: Tue, 7 Nov 2023 17:39:53 +0700 Subject: [PATCH 2/3] connect FE to textai server --- .../jspf/guest/components/foodDetails.jspf | 21 +++++++++++++++++-- .../jspf/hyperfeat/fooddetechbutton.jspf | 5 +++-- 2 files changed, 22 insertions(+), 4 deletions(-) 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 8dde8c23..e1bb06df 100644 --- a/src/main/webapp/WEB-INF/jspf/guest/components/foodDetails.jspf +++ b/src/main/webapp/WEB-INF/jspf/guest/components/foodDetails.jspf @@ -181,8 +181,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 a635e0c3..15507f5d 100644 --- a/src/main/webapp/WEB-INF/jspf/hyperfeat/fooddetechbutton.jspf +++ b/src/main/webapp/WEB-INF/jspf/hyperfeat/fooddetechbutton.jspf @@ -106,10 +106,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; + const searchURL = "http://localhost:8001/search_food_by_name/" + data.predicted_dish[0][0]; const searchResponse = await fetch(searchURL); @@ -126,6 +126,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 = 'Mô tả món ăn.'; closeModalButton.click(); } From 2301271607f10c95e80fbcf8af7ad1834d5713d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ti=E1=BA=BFn=20Th=C3=A0nh=20H=E1=BB=A9a?= Date: Tue, 7 Nov 2023 17:52:53 +0700 Subject: [PATCH 3/3] feat: Update loading text --- src/main/webapp/WEB-INF/jspf/guest/components/foodDetails.jspf | 2 +- src/main/webapp/WEB-INF/jspf/hyperfeat/fooddetechbutton.jspf | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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 e1bb06df..f520eb3d 100644 --- a/src/main/webapp/WEB-INF/jspf/guest/components/foodDetails.jspf +++ b/src/main/webapp/WEB-INF/jspf/guest/components/foodDetails.jspf @@ -63,7 +63,7 @@

Mô tả món ăn.

+ class="card-text fs-1">Đang cập nhật mô tả...

diff --git a/src/main/webapp/WEB-INF/jspf/hyperfeat/fooddetechbutton.jspf b/src/main/webapp/WEB-INF/jspf/hyperfeat/fooddetechbutton.jspf index 15507f5d..9c2f2cab 100644 --- a/src/main/webapp/WEB-INF/jspf/hyperfeat/fooddetechbutton.jspf +++ b/src/main/webapp/WEB-INF/jspf/hyperfeat/fooddetechbutton.jspf @@ -126,7 +126,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 = 'Mô tả món ăn.'; + foodDetailsModal.querySelector("#food-description").innerHTML = 'Đang cập nhật mô tả...'; closeModalButton.click(); }