Spaces:
Running
Running
| from fasthtml.common import * | |
| from faq import create_faq | |
| import pandas as pd | |
| from fasthtml_hf import setup_hf_backup | |
| df = pd.read_csv("./ar_en_nutrition.csv") | |
| from food_sql import create_food_model, update_database | |
| app, rt, foods, Food = create_food_model(df) | |
| try: | |
| app, rt, foods, Food = create_food_model(df) | |
| update_database(df) | |
| print("Database updated successfully!") | |
| except Exception as e: | |
| print(f"Error updating database: {e}") | |
| items = foods() | |
| print(len(items)) | |
| for item in items[:5]: | |
| print(item.arabic_name, item.name, item.calories) | |
| el = foods.search("banana") | |
| for e in el: | |
| print(e["name"]) | |
| print(e["arabic_name"]) | |
| print(e["calories"]) | |
| print(e) | |
| break | |
| seo_meta = [ | |
| Meta( | |
| name="description", | |
| content="كم كالوري أفضل موقع لمعرفة عدد السعرات الحرارية في الأطعمة باستخدام البحث الذكي وتقنيات الذكاء الإصطناعي", | |
| ), | |
| Meta(name="keywords", content="سعرات حرارية, غذاء, صحة, تغذية, حمية"), | |
| Meta(name="author", content="Abdelkareem Elkhateb"), | |
| Meta(property="og:title", content="كم كالوري - حاسبة السعرات الحرارية"), | |
| Meta(property="og:description", content="احسب السعرات الحرارية في طعامك بسهولة"), | |
| Meta(property="og:type", content="website"), | |
| Meta(name="robots", content="index, follow"), | |
| Meta(name="language", content="ar"), | |
| Meta(charset="UTF-8"), | |
| # Meta(property="og:url", content="your-website-url"), | |
| # Meta(property="og:image", content="url-to-your-logo"), | |
| Meta(name="twitter:card", content="summary"), | |
| Meta(name="twitter:title", content="كم كالوري - حاسبة السعرات الحرارية"), | |
| Meta(name="twitter:description", content="احسب السعرات الحرارية في طعامك بسهولة"), | |
| # Meta(name="twitter:image", content="url-to-your-logo"), | |
| # Additional SEO | |
| Meta(name="theme-color", content="#4a90e2"), | |
| Meta(name="application-name", content="كم كالوري"), | |
| Meta(property="og:locale", content="ar_AR"), | |
| Meta(name="viewport", content="width=device-width, initial-scale=1.0"), | |
| ] | |
| app, rt, foods, Food = fast_app( | |
| "data/foods.db", | |
| hdrs=[ | |
| *seo_meta, | |
| Style(""" | |
| :root { | |
| --pico-font-size: 100%; | |
| --pico-color-info: #4a90e2; | |
| } | |
| body { | |
| direction: rtl; | |
| text-align: right; | |
| } | |
| .navbar { | |
| background-color: var(--pico-color-info); | |
| padding: 1rem; | |
| margin-bottom: 2rem; | |
| } | |
| .navbar a { | |
| color: white; | |
| text-decoration: none; | |
| } | |
| table a { | |
| text-decoration: none; | |
| } | |
| """), | |
| ], | |
| id=int, | |
| name=str, | |
| serving_size=str, | |
| calories=int, | |
| total_fat=str, | |
| saturated_fat=str, | |
| cholesterol=str, | |
| sodium=str, | |
| choline=str, | |
| folate=str, | |
| folic_acid=str, | |
| niacin=str, | |
| pantothenic_acid=str, | |
| riboflavin=str, | |
| thiamin=str, | |
| vitamin_a=str, | |
| vitamin_a_rae=str, | |
| carotene_alpha=str, | |
| carotene_beta=str, | |
| cryptoxanthin_beta=str, | |
| lutein_zeaxanthin=str, | |
| lucopene=int, | |
| vitamin_b12=str, | |
| vitamin_b6=str, | |
| vitamin_c=str, | |
| vitamin_d=str, | |
| vitamin_e=str, | |
| tocopherol_alpha=str, | |
| vitamin_k=str, | |
| calcium=str, | |
| copper=str, | |
| irom=str, | |
| magnesium=str, | |
| manganese=str, | |
| phosphorous=str, | |
| potassium=str, | |
| selenium=str, | |
| zink=str, | |
| protein=str, | |
| alanine=str, | |
| arginine=str, | |
| aspartic_acid=str, | |
| cystine=str, | |
| glutamic_acid=str, | |
| glycine=str, | |
| histidine=str, | |
| hydroxyproline=str, | |
| isoleucine=str, | |
| leucine=str, | |
| lysine=str, | |
| methionine=str, | |
| phenylalanine=str, | |
| proline=str, | |
| serine=str, | |
| threonine=str, | |
| tryptophan=str, | |
| tyrosine=str, | |
| valine=str, | |
| carbohydrate=str, | |
| fiber=str, | |
| sugars=str, | |
| fructose=str, | |
| galactose=str, | |
| glucose=str, | |
| lactose=str, | |
| maltose=str, | |
| sucrose=str, | |
| fat=str, | |
| saturated_fatty_acids=str, | |
| monounsaturated_fatty_acids=str, | |
| polyunsaturated_fatty_acids=str, | |
| fatty_acids_total_trans=str, | |
| alcohol=str, | |
| ash=str, | |
| caffeine=str, | |
| theobromine=str, | |
| water=str, | |
| arabic_name=str, | |
| pk="id", | |
| ) | |
| def mk_search_input(): | |
| return Input( | |
| id="search-input", name="query", placeholder="ابحث عن الطعام..", required=True | |
| ) | |
| def post(query: str): | |
| results = foods.search(query, limit=20) | |
| header = Thead( | |
| Tr( | |
| *map(Th, ["الاسم بالعربية", "Food", "السعرات الحرارية"]), | |
| ), | |
| cls="bg-purple/10", | |
| ) | |
| rows = [ | |
| Tr( | |
| Td( | |
| AX( | |
| f["arabic_name"], | |
| f"/food/{f['id']}", | |
| "results-table", | |
| hx_push_url="true", | |
| ) | |
| ), | |
| Td(f["name"]), | |
| Td(f["calories"]), | |
| cls="even:bg-purple/5", | |
| ) | |
| for f in results | |
| ] | |
| return Table(header, *rows, cls="w-full", id="results-table") | |
| def get(id: int): | |
| food = foods.get(id) | |
| return Table( | |
| *[Tr(Th(k), Td(v)) for k, v in food.__dict__.items() if k != "id"], | |
| id="results-table", | |
| ) | |
| def get(): | |
| header = Header( | |
| Nav( | |
| Ul( | |
| Li(A("البحث الذكي", href="/")), | |
| ), | |
| cls="container", | |
| ), | |
| cls="navbar", | |
| ) | |
| search_form = Form( | |
| Group(mk_search_input(), Button("Search")), | |
| hx_post="/search", | |
| target_id="results-table", | |
| ) | |
| return ( | |
| Title("كم كالوري"), | |
| header, | |
| Main( | |
| H1("كم كالوري في .."), | |
| search_form, | |
| Div(id="results-table"), | |
| create_faq(), | |
| cls="container", | |
| ), | |
| ) | |
| setup_hf_backup(app) | |
| if __name__ == "__main__": | |
| serve() | |