Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
3 |
+
import torch
|
4 |
+
import spaces
|
5 |
+
|
6 |
+
def get_model_name(language):
|
7 |
+
"""Map language choice to the corresponding model."""
|
8 |
+
model_mapping = {
|
9 |
+
"English": "microsoft/Phi-3-mini-4k-instruct",
|
10 |
+
"Arabic": "ALLaM-AI/ALLaM-7B-Instruct-preview"
|
11 |
+
}
|
12 |
+
return model_mapping.get(language, "ALLaM-AI/ALLaM-7B-Instruct-preview") # Default to Arabic model
|
13 |
+
|
14 |
+
def load_model(model_name):
|
15 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
16 |
+
model = AutoModelForCausalLM.from_pretrained(
|
17 |
+
model_name,
|
18 |
+
device_map=device,
|
19 |
+
torch_dtype="auto",
|
20 |
+
trust_remote_code=True,
|
21 |
+
)
|
22 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
23 |
+
generator = pipeline(
|
24 |
+
"text-generation",
|
25 |
+
model=model,
|
26 |
+
tokenizer=tokenizer,
|
27 |
+
return_full_text=False,
|
28 |
+
max_new_tokens=500,
|
29 |
+
do_sample=False
|
30 |
+
)
|
31 |
+
return generator
|
32 |
+
|
33 |
+
@spaces.GPU
|
34 |
+
def generate_plant_info(plant_name, language):
|
35 |
+
model_name = get_model_name(language)
|
36 |
+
generator = load_model(model_name)
|
37 |
+
|
38 |
+
# Define prompt for the AI model
|
39 |
+
if language == "English":
|
40 |
+
prompt = (f"Provide detailed information about {plant_name}. "
|
41 |
+
f"Include its scientific name, growing conditions (light, water, soil type), "
|
42 |
+
f"common uses, and care tips.")
|
43 |
+
else:
|
44 |
+
prompt = (f"قدم معلومات مفصلة عن {plant_name}. "
|
45 |
+
f"اذكر اسمه العلمي، وظروف نموه (الضوء، الماء، نوع التربة)، "
|
46 |
+
f"استخداماته الشائعة، ونصائح العناية به.")
|
47 |
+
|
48 |
+
messages = [{"role": "user", "content": prompt}]
|
49 |
+
output = generator(messages)
|
50 |
+
return output[0]["generated_text"]
|
51 |
+
|
52 |
+
# Create Gradio Blocks interface
|
53 |
+
with gr.Blocks(theme="soft") as demo:
|
54 |
+
gr.Markdown("<h1 style='text-align: center; color: #2E8B57;'>🌿 AI Plant Guide - English & Arabic 🌿</h1>")
|
55 |
+
gr.Markdown("<p style='text-align: center; font-size: 18px;'>Enter a plant name, and AI will provide detailed information about it in English or Arabic.</p>")
|
56 |
+
|
57 |
+
# Language selection
|
58 |
+
language_selector = gr.Radio(["English", "Arabic"], label="🌍 Choose Language", value="English")
|
59 |
+
|
60 |
+
# Plant name input
|
61 |
+
plant_name_input = gr.Textbox(placeholder="Enter plant name (e.g., Lavender, Aloe Vera)...", label="Plant Name")
|
62 |
+
|
63 |
+
output_text = gr.Textbox(label="Plant Information", interactive=False)
|
64 |
+
|
65 |
+
# Example button functionality
|
66 |
+
example_plants = [
|
67 |
+
("Lavender", "English"),
|
68 |
+
("اللافندر", "Arabic"),
|
69 |
+
("Tulip", "English"),
|
70 |
+
("الصبار", "Arabic"),
|
71 |
+
]
|
72 |
+
|
73 |
+
def update_inputs(plant_name, language):
|
74 |
+
return plant_name, language
|
75 |
+
|
76 |
+
with gr.Row():
|
77 |
+
for name, lang in example_plants:
|
78 |
+
example_button = gr.Button(f"🌿 {name} ({lang})")
|
79 |
+
|
80 |
+
# Use lambda to pass the arguments directly
|
81 |
+
example_button.click(lambda plant_name=name, language=lang: update_inputs(plant_name, language),
|
82 |
+
outputs=[plant_name_input, language_selector])
|
83 |
+
|
84 |
+
classify_button = gr.Button("🔍 Get Plant Info", variant="primary")
|
85 |
+
classify_button.click(generate_plant_info, inputs=[plant_name_input, language_selector], outputs=output_text)
|
86 |
+
|
87 |
+
# Run the application
|
88 |
+
if __name__ == "__main__":
|
89 |
+
demo.launch()
|