import gradio as gr from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline import torch import spaces def get_model_name(language): """Map language choice to the corresponding model.""" model_mapping = { "English": "microsoft/Phi-3-mini-4k-instruct", "Arabic": "ALLaM-AI/ALLaM-7B-Instruct-preview" } return model_mapping.get(language, "ALLaM-AI/ALLaM-7B-Instruct-preview") # Default to Arabic model def load_model(model_name): device = "cuda" if torch.cuda.is_available() else "cpu" model = AutoModelForCausalLM.from_pretrained( model_name, device_map=device, torch_dtype="auto", trust_remote_code=True, ) tokenizer = AutoTokenizer.from_pretrained(model_name) generator = pipeline( "text-generation", model=model, tokenizer=tokenizer, return_full_text=False, max_new_tokens=500, do_sample=False ) return generator @spaces.GPU def generate_plant_info(plant_name, language): model_name = get_model_name(language) generator = load_model(model_name) # Define prompt for the AI model 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 care tips.") else: prompt = (f"قدم معلومات مفصلة عن {plant_name}. " f"اذكر اسمه العلمي، وظروف نموه (الضوء، الماء، نوع التربة)، " f"استخداماته الشائعة، ونصائح العناية به.") messages = [{"role": "user", "content": prompt}] output = generator(messages) return output[0]["generated_text"] # Create Gradio interface with enhancements custom_css = """ body { font-family: Arial, sans-serif; background-color: #f9f9f9; } .gri-container { border-radius: 10px; background-color: #fff; box-shadow: 0 4px 20px rgba(0,0,0,0.1); } .gri-button { background-color: #4CAF50; color: white; } .gri-button:hover { background-color: #45a049; } """ demo = gr.Interface( fn=generate_plant_info, inputs=[ gr.Textbox(placeholder="Enter plant name (e.g., Lavender, Aloe Vera)...", label="Plant Name"), gr.Dropdown( choices=["English", "Arabic"], label="Choose Language", value="English" ) ], outputs=gr.Textbox(label="Plant Information"), title="🌿 AI Plant Guide - English & Arabic 🌿", description="Enter a plant name, and AI will provide detailed information about it in English or Arabic.", examples=[ ["Lavender", "English"], ["اللافندر", "Arabic"], ["Tulip", "English"], ["الصبار", "Arabic"] ], theme="default", css=custom_css, # Add custom CSS here ) demo.launch(share=True) # Launch the interface without css parameter