|
import gradio as gr |
|
import pandas as pd |
|
import speech_recognition as sr |
|
pip install gradio pandas speechrecognition pyaudio |
|
|
|
|
|
def load_recipes(file_path): |
|
df = pd.read_csv(file_path) |
|
recipes = [] |
|
for _, row in df.iterrows(): |
|
recipes.append({ |
|
"name": row["name"], |
|
"ingredients": [i.strip() for i in row["ingredients"].split(',')], |
|
"steps": row["steps"] |
|
}) |
|
return recipes |
|
|
|
|
|
recipes = load_recipes('recipes_arabic.csv') |
|
|
|
def suggest_recipes(user_ingredients): |
|
user_ingredients = [i.strip().lower() for i in user_ingredients.split(',')] |
|
matching_recipes = [] |
|
for recipe in recipes: |
|
recipe_ingredients = [i.strip().lower() for i in recipe['ingredients']] |
|
if all(ingredient in recipe_ingredients for ingredient in user_ingredients): |
|
ingredient_list = ', '.join(recipe['ingredients']) |
|
matching_recipes.append(f"{recipe['name']}\nIngredients: {ingredient_list}\nSteps: {recipe['steps']}") |
|
if not matching_recipes: |
|
return "لا توجد وصفات تتناسب مع المكونات المدخلة." |
|
return "\n\n".join(matching_recipes) |
|
|
|
|
|
def audio_to_text(audio_file): |
|
recognizer = sr.Recognizer() |
|
with sr.AudioFile(audio_file) as source: |
|
audio_data = recognizer.record(source) |
|
try: |
|
text = recognizer.recognize_google(audio_data, language="ar") |
|
return text |
|
except sr.UnknownValueError: |
|
return "لم أتمكن من فهم الصوت." |
|
except sr.RequestError: |
|
return "حدث خطأ في الاتصال بخدمة تحويل الصوت إلى نص." |
|
|
|
def process_input(text_input, audio_file): |
|
if audio_file: |
|
user_ingredients = audio_to_text(audio_file) |
|
else: |
|
user_ingredients = text_input |
|
|
|
if not user_ingredients: |
|
return "يرجى إدخال المكونات." |
|
|
|
return suggest_recipes(user_ingredients) |
|
|
|
|
|
iface = gr.Interface( |
|
fn=process_input, |
|
inputs=[ |
|
gr.Textbox(label="ادخل المكونات (مفصولة بفواصل)", placeholder="أدخل المكونات هنا", optional=True), |
|
gr.Audio(source="microphone", type="filepath", label="تحدث لتحديد المكونات", optional=True) |
|
], |
|
outputs=gr.Textbox(label="الوصفات المقترحة"), |
|
title="اقتراحات الوصفات العربية", |
|
description="ادخل المكونات التي لديك للحصول على اقتراحات لوصفات عربية تقليدية. يمكنك أيضاً التحدث لتحديد المكونات." |
|
) |
|
|
|
if __name__ == "__main__": |
|
iface.launch() |
|
|