import gradio as gr from transformers import pipeline # Load models gen_model = pipeline("text2text-generation", model="google/flan-t5-large") translator_en_ar = pipeline("translation", model="Helsinki-NLP/opus-mt-en-ar") # English to Arabic translator_ar_en = pipeline("translation", model="Helsinki-NLP/opus-mt-ar-en") # Arabic to English tokenizer = AutoTokenizer.from_pretrained("Helsinki-NLP/opus-mt-en-ar") def get_plant_info(plant_name, language): if language == "English": prompt = ( f"Provide detailed information about {plant_name}. " f"Include its scientific name, growing conditions (light, water, soil type), " f"common uses, and how to take care of it." ) response = gen_model(prompt, min_length=50, max_length=300)[0]["generated_text"] else: # Arabic translated_name = translator_ar_en(plant_name)[0]["translation_text"] # Convert Arabic input to English prompt = ( f"Provide detailed information about {translated_name}. " f"Include its scientific name, growing conditions (light, water, soil type), " f"common uses, and how to take care of it." ) response_en = gen_model(prompt, min_length=50, max_length=300)[0]["generated_text"] response = translator_en_ar(response_en)[0]["translation_text"] # Convert English output back to Arabic return response # Gradio UI interface = gr.Interface( fn=get_plant_info, inputs=[ gr.Textbox(label="Enter Plant Name / أدخل اسم النبات"), gr.Radio(["English", "العربية"], label="Choose Language / اختر اللغة") ], outputs=gr.Textbox(label="Plant Information / معلومات النبات", lines=10), title="Plant Information App", description="Enter a plant name and select a language to get detailed information." ) # Launch the app if __name__ == "__main__": demo.launch()