Spaces:
Sleeping
Sleeping
Abdelkareem
commited on
Create main.py
Browse files
main.py
ADDED
@@ -0,0 +1,214 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fasthtml.common import *
|
2 |
+
from faq import create_faq
|
3 |
+
from fasthtml_hf import setup_hf_backup
|
4 |
+
|
5 |
+
seo_meta = [
|
6 |
+
Meta(
|
7 |
+
name="description",
|
8 |
+
content="كم كالوري أفضل موقع لمعرفة عدد السعرات الحرارية في الأطعمة باستخدام البحث الذكي وتقنيات الذكاء الإصطناعي",
|
9 |
+
),
|
10 |
+
Meta(name="keywords", content="سعرات حرارية, غذاء, صحة, تغذية, حمية"),
|
11 |
+
Meta(name="author", content="Abdelkareem Elkhateb"),
|
12 |
+
Meta(property="og:title", content="كم كالوري - حاسبة السعرات الحرارية"),
|
13 |
+
Meta(property="og:description", content="احسب السعرات الحرارية في طعامك بسهولة"),
|
14 |
+
Meta(property="og:type", content="website"),
|
15 |
+
Meta(name="robots", content="index, follow"),
|
16 |
+
Meta(name="language", content="ar"),
|
17 |
+
Meta(charset="UTF-8"),
|
18 |
+
# Meta(property="og:url", content="your-website-url"),
|
19 |
+
# Meta(property="og:image", content="url-to-your-logo"),
|
20 |
+
Meta(name="twitter:card", content="summary"),
|
21 |
+
Meta(name="twitter:title", content="كم كالوري - حاسبة السعرات الحرارية"),
|
22 |
+
Meta(name="twitter:description", content="احسب السعرات الحرارية في طعامك بسهولة"),
|
23 |
+
# Meta(name="twitter:image", content="url-to-your-logo"),
|
24 |
+
# Additional SEO
|
25 |
+
Meta(name="theme-color", content="#4a90e2"),
|
26 |
+
Meta(name="application-name", content="كم كالوري"),
|
27 |
+
Meta(property="og:locale", content="ar_AR"),
|
28 |
+
Meta(name="viewport", content="width=device-width, initial-scale=1.0"),
|
29 |
+
]
|
30 |
+
app, rt, foods, Food = fast_app(
|
31 |
+
"data/foods.db",
|
32 |
+
hdrs=[
|
33 |
+
*seo_meta,
|
34 |
+
Style("""
|
35 |
+
:root {
|
36 |
+
--pico-font-size: 100%;
|
37 |
+
--pico-color-info: #4a90e2;
|
38 |
+
}
|
39 |
+
body {
|
40 |
+
direction: rtl;
|
41 |
+
text-align: right;
|
42 |
+
}
|
43 |
+
.navbar {
|
44 |
+
background-color: var(--pico-color-info);
|
45 |
+
padding: 1rem;
|
46 |
+
margin-bottom: 2rem;
|
47 |
+
}
|
48 |
+
.navbar a {
|
49 |
+
color: white;
|
50 |
+
text-decoration: none;
|
51 |
+
}
|
52 |
+
table a {
|
53 |
+
text-decoration: none;
|
54 |
+
}
|
55 |
+
"""),
|
56 |
+
],
|
57 |
+
id=int,
|
58 |
+
name=str,
|
59 |
+
serving_size=str,
|
60 |
+
calories=int,
|
61 |
+
total_fat=str,
|
62 |
+
saturated_fat=str,
|
63 |
+
cholesterol=str,
|
64 |
+
sodium=str,
|
65 |
+
choline=str,
|
66 |
+
folate=str,
|
67 |
+
folic_acid=str,
|
68 |
+
niacin=str,
|
69 |
+
pantothenic_acid=str,
|
70 |
+
riboflavin=str,
|
71 |
+
thiamin=str,
|
72 |
+
vitamin_a=str,
|
73 |
+
vitamin_a_rae=str,
|
74 |
+
carotene_alpha=str,
|
75 |
+
carotene_beta=str,
|
76 |
+
cryptoxanthin_beta=str,
|
77 |
+
lutein_zeaxanthin=str,
|
78 |
+
lucopene=int,
|
79 |
+
vitamin_b12=str,
|
80 |
+
vitamin_b6=str,
|
81 |
+
vitamin_c=str,
|
82 |
+
vitamin_d=str,
|
83 |
+
vitamin_e=str,
|
84 |
+
tocopherol_alpha=str,
|
85 |
+
vitamin_k=str,
|
86 |
+
calcium=str,
|
87 |
+
copper=str,
|
88 |
+
irom=str,
|
89 |
+
magnesium=str,
|
90 |
+
manganese=str,
|
91 |
+
phosphorous=str,
|
92 |
+
potassium=str,
|
93 |
+
selenium=str,
|
94 |
+
zink=str,
|
95 |
+
protein=str,
|
96 |
+
alanine=str,
|
97 |
+
arginine=str,
|
98 |
+
aspartic_acid=str,
|
99 |
+
cystine=str,
|
100 |
+
glutamic_acid=str,
|
101 |
+
glycine=str,
|
102 |
+
histidine=str,
|
103 |
+
hydroxyproline=str,
|
104 |
+
isoleucine=str,
|
105 |
+
leucine=str,
|
106 |
+
lysine=str,
|
107 |
+
methionine=str,
|
108 |
+
phenylalanine=str,
|
109 |
+
proline=str,
|
110 |
+
serine=str,
|
111 |
+
threonine=str,
|
112 |
+
tryptophan=str,
|
113 |
+
tyrosine=str,
|
114 |
+
valine=str,
|
115 |
+
carbohydrate=str,
|
116 |
+
fiber=str,
|
117 |
+
sugars=str,
|
118 |
+
fructose=str,
|
119 |
+
galactose=str,
|
120 |
+
glucose=str,
|
121 |
+
lactose=str,
|
122 |
+
maltose=str,
|
123 |
+
sucrose=str,
|
124 |
+
fat=str,
|
125 |
+
saturated_fatty_acids=str,
|
126 |
+
monounsaturated_fatty_acids=str,
|
127 |
+
polyunsaturated_fatty_acids=str,
|
128 |
+
fatty_acids_total_trans=str,
|
129 |
+
alcohol=str,
|
130 |
+
ash=str,
|
131 |
+
caffeine=str,
|
132 |
+
theobromine=str,
|
133 |
+
water=str,
|
134 |
+
arabic_name=str,
|
135 |
+
pk="id",
|
136 |
+
)
|
137 |
+
|
138 |
+
|
139 |
+
def mk_search_input():
|
140 |
+
return Input(
|
141 |
+
id="search-input", name="query", placeholder="ابحث عن الطعام..", required=True
|
142 |
+
)
|
143 |
+
|
144 |
+
|
145 |
+
@rt("/search")
|
146 |
+
def post(query: str):
|
147 |
+
results = foods.search(query, limit=20)
|
148 |
+
header = Thead(
|
149 |
+
Tr(
|
150 |
+
*map(Th, ["الاسم بالعربية", "Food", "السعرات الحرارية"]),
|
151 |
+
),
|
152 |
+
cls="bg-purple/10",
|
153 |
+
)
|
154 |
+
|
155 |
+
rows = [
|
156 |
+
Tr(
|
157 |
+
Td(
|
158 |
+
AX(
|
159 |
+
f["arabic_name"],
|
160 |
+
f"/food/{f['id']}",
|
161 |
+
"results-table",
|
162 |
+
hx_push_url="true",
|
163 |
+
)
|
164 |
+
),
|
165 |
+
Td(f["name"]),
|
166 |
+
Td(f["calories"]),
|
167 |
+
cls="even:bg-purple/5",
|
168 |
+
)
|
169 |
+
for f in results
|
170 |
+
]
|
171 |
+
return Table(header, *rows, cls="w-full", id="results-table")
|
172 |
+
|
173 |
+
|
174 |
+
@rt("/food/{id}")
|
175 |
+
def get(id: int):
|
176 |
+
food = foods.get(id)
|
177 |
+
return Table(
|
178 |
+
*[Tr(Th(k), Td(v)) for k, v in food.__dict__.items() if k != "id"],
|
179 |
+
id="results-table",
|
180 |
+
)
|
181 |
+
|
182 |
+
|
183 |
+
@rt("/")
|
184 |
+
def get():
|
185 |
+
header = Header(
|
186 |
+
Nav(
|
187 |
+
Ul(
|
188 |
+
Li(A("البحث الذكي", href="/")),
|
189 |
+
),
|
190 |
+
cls="container",
|
191 |
+
),
|
192 |
+
cls="navbar",
|
193 |
+
)
|
194 |
+
search_form = Form(
|
195 |
+
Group(mk_search_input(), Button("Search")),
|
196 |
+
hx_post="/search",
|
197 |
+
target_id="results-table",
|
198 |
+
)
|
199 |
+
return (
|
200 |
+
Title("كم كالوري"),
|
201 |
+
header,
|
202 |
+
Main(
|
203 |
+
H1("كم كالوري في .."),
|
204 |
+
search_form,
|
205 |
+
Div(id="results-table"),
|
206 |
+
create_faq(),
|
207 |
+
cls="container",
|
208 |
+
),
|
209 |
+
)
|
210 |
+
|
211 |
+
setup_hf_backup(app)
|
212 |
+
|
213 |
+
if __name__ == "__main__":
|
214 |
+
serve()
|