Spaces:
Sleeping
Sleeping
Abdelkareem
commited on
Commit
•
0cb23eb
1
Parent(s):
32e73ba
Upload 4 files
Browse files- ar_en_nutrition.csv +0 -0
- faq.py +24 -0
- food_sql.py +69 -0
- requirements.txt +3 -0
ar_en_nutrition.csv
ADDED
The diff for this file is too large to render.
See raw diff
|
|
faq.py
ADDED
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fasthtml.common import *
|
2 |
+
|
3 |
+
|
4 |
+
def create_faq():
|
5 |
+
faq_items = [
|
6 |
+
(
|
7 |
+
"كيف يمكنني البحث عن الطعام؟",
|
8 |
+
"يمكنك كتابة اسم الطعام باللغة العربية أو الإنجليزية في مربع البحث",
|
9 |
+
),
|
10 |
+
(
|
11 |
+
"ما هي وحدة قياس السعرات الحرارية؟",
|
12 |
+
"السعرات الحرارية مقاسة لكل 100 جرام من الطعام",
|
13 |
+
),
|
14 |
+
("هل المعلومات دقيقة؟", "نعم، المعلومات مأخوذة من قاعدة بيانات موثوقة للأغذية"),
|
15 |
+
]
|
16 |
+
faq_list = [
|
17 |
+
Details(Summary(question), P(answer), cls="faq-item")
|
18 |
+
for question, answer in faq_items
|
19 |
+
]
|
20 |
+
return Section(
|
21 |
+
H2("الأسئلة الشائعة عن السعرات الحرارية"),
|
22 |
+
*faq_list,
|
23 |
+
cls="faq-section container",
|
24 |
+
)
|
food_sql.py
ADDED
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
from fasthtml.common import *
|
3 |
+
|
4 |
+
df = pd.read_csv(
|
5 |
+
"ar_en_nutrition.csv",
|
6 |
+
index_col=0,
|
7 |
+
)
|
8 |
+
|
9 |
+
|
10 |
+
def create_food_model(df):
|
11 |
+
# Remove the 'Unnamed: 0' column if it exists
|
12 |
+
if "Unnamed: 0" in df.columns:
|
13 |
+
df = df.drop("Unnamed: 0", axis=1)
|
14 |
+
|
15 |
+
# Create a dictionary of column names and their types
|
16 |
+
dtype_map = {"int64": int, "object": str, "float64": float}
|
17 |
+
|
18 |
+
# Start with the id column
|
19 |
+
column_types = {"id": int}
|
20 |
+
|
21 |
+
# Add other columns, excluding any id column from the DataFrame
|
22 |
+
for col in df.columns:
|
23 |
+
if col.lower() != "id":
|
24 |
+
column_types[col] = dtype_map[str(df[col].dtype)]
|
25 |
+
|
26 |
+
# Create the app with dynamically generated columns
|
27 |
+
return fast_app(
|
28 |
+
"data/foods.db",
|
29 |
+
hdrs=[Style(":root { --pico-font-size: 100%; }")],
|
30 |
+
pk="id",
|
31 |
+
**column_types,
|
32 |
+
)
|
33 |
+
|
34 |
+
|
35 |
+
def update_database(df):
|
36 |
+
# Create the model based on DataFrame structure
|
37 |
+
app, rt, foods, Food = create_food_model(df)
|
38 |
+
|
39 |
+
# Convert DataFrame rows to Food objects, letting SQLite handle the ID
|
40 |
+
for _, row in df.iterrows():
|
41 |
+
row_dict = row.to_dict()
|
42 |
+
if "id" in row_dict:
|
43 |
+
del row_dict["id"]
|
44 |
+
if "Unnamed: 0" in row_dict:
|
45 |
+
del row_dict["Unnamed: 0"]
|
46 |
+
foods.insert(Food(**row_dict))
|
47 |
+
foods.enable_fts(["name", "arabic_name"])
|
48 |
+
|
49 |
+
|
50 |
+
try:
|
51 |
+
app, rt, foods, Food = create_food_model(df)
|
52 |
+
update_database(df)
|
53 |
+
print("Database updated successfully!")
|
54 |
+
except Exception as e:
|
55 |
+
print(f"Error updating database: {e}")
|
56 |
+
|
57 |
+
items = foods()
|
58 |
+
print(len(items))
|
59 |
+
|
60 |
+
# for item in items[:5]:
|
61 |
+
# print(item.arabic_name, item.name, item.calories)
|
62 |
+
|
63 |
+
# el = foods.search("banana")
|
64 |
+
# for e in el:
|
65 |
+
# print(e["name"])
|
66 |
+
# print(e["arabic_name"])
|
67 |
+
# print(e["calories"])
|
68 |
+
# print(e)
|
69 |
+
# break
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
python-fasthtml
|
2 |
+
fasthtml-hf
|
3 |
+
uvicorn
|