File size: 2,972 Bytes
37d987e
2188d3f
 
37d987e
2188d3f
 
 
 
 
 
 
37d987e
2188d3f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d2ab18f
69616d7
 
2188d3f
69616d7
 
 
 
 
 
 
 
 
 
 
2188d3f
 
 
37d987e
69616d7
 
 
 
 
 
 
 
 
 
 
 
2188d3f
 
69616d7
37d987e
69616d7
2188d3f
 
 
69616d7
2188d3f
37d987e
69616d7
 
 
2188d3f
69616d7
 
 
 
 
 
 
37d987e
 
69616d7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
import torch

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"]

# Custom CSS for styling
custom_css = """
.gradio-container {
    max-width: 700px !important;
    margin: auto !important;
    padding: 20px !important;
    background: #ffffff !important;
    border-radius: 12px !important;
    box-shadow: 0px 4px 10px rgba(0, 0, 0, 0.1) !important;
}
"""

# Create Gradio interface
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"],  # Users choose the language, not the model name
            label="Choose Language",
            value="English"  # Default to 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"],
        ["Rose", "English"],
        ["الشيح", "Arabic"]
    ],
    theme="default",
    css=custom_css  # Apply custom styles
)

demo.launch()