Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

udpate textai server #162

Merged
merged 4 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 5 additions & 11 deletions api/psqlserver/model/Food.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion api/psqlserver/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ google-auth
Unidecode
wikipedia
pytz
bardapi
g4f
40 changes: 40 additions & 0 deletions api/textai_server/app.py
Original file line number Diff line number Diff line change
@@ -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')
6 changes: 6 additions & 0 deletions api/textai_server/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
uvicorn
pathlib2
requests
fastapi
protobuf==3.20
g4f
24 changes: 21 additions & 3 deletions src/main/webapp/WEB-INF/jspf/guest/components/foodDetails.jspf
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
</div>

<!-- Food description -->
<p id="food-description" class="card-text fs-1">Mô tả món ăn.</p>
<p id="food-description"
class="card-text fs-1">Đang cập nhật mô tả...</p>

<!-- Empty space -->
<div class="flex-grow-1"></div>
Expand Down Expand Up @@ -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") {
Expand Down
4 changes: 2 additions & 2 deletions src/main/webapp/WEB-INF/jspf/hyperfeat/fooddetechbutton.jspf
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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();
}

Expand Down