Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,44 +1,59 @@
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import pipeline
|
|
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
|
|
|
|
9 |
|
10 |
-
def
|
11 |
-
if
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
|
|
27 |
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
-
# Gradio
|
31 |
-
|
32 |
-
fn=
|
33 |
inputs=[
|
34 |
-
gr.Textbox(
|
35 |
-
gr.
|
|
|
|
|
|
|
|
|
36 |
],
|
37 |
-
outputs=gr.Textbox(label="
|
38 |
-
title="
|
39 |
-
description="Enter a
|
|
|
|
|
|
|
|
|
40 |
)
|
41 |
|
42 |
-
|
43 |
-
if __name__ == "__main__":
|
44 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
3 |
+
import torch
|
4 |
|
5 |
+
def get_model_name(language):
|
6 |
+
"""Map language choice to the corresponding model."""
|
7 |
+
model_mapping = {
|
8 |
+
"English": "microsoft/Phi-3-mini-4k-instruct",
|
9 |
+
"Arabic": "ALLaM-AI/ALLaM-7B-Instruct-preview"
|
10 |
+
}
|
11 |
+
return model_mapping.get(language, "ALLaM-AI/ALLaM-7B-Instruct-preview") # Default to Arabic model
|
12 |
|
13 |
+
def load_model(model_name):
|
14 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
15 |
+
model = AutoModelForCausalLM.from_pretrained(
|
16 |
+
model_name,
|
17 |
+
device_map=device,
|
18 |
+
torch_dtype="auto",
|
19 |
+
trust_remote_code=True,
|
20 |
+
)
|
21 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
22 |
+
generator = pipeline(
|
23 |
+
"text-generation",
|
24 |
+
model=model,
|
25 |
+
tokenizer=tokenizer,
|
26 |
+
return_full_text=False,
|
27 |
+
max_new_tokens=500,
|
28 |
+
do_sample=False
|
29 |
+
)
|
30 |
+
return generator
|
31 |
|
32 |
+
def generate_text(prompt, language):
|
33 |
+
model_name = get_model_name(language) # Get the model based on language choice
|
34 |
+
generator = load_model(model_name)
|
35 |
+
messages = [{"role": "user", "content": prompt}]
|
36 |
+
output = generator(messages)
|
37 |
+
return output[0]["generated_text"]
|
38 |
|
39 |
+
# Create Gradio interface
|
40 |
+
demo = gr.Interface(
|
41 |
+
fn=generate_text,
|
42 |
inputs=[
|
43 |
+
gr.Textbox(lines=2, placeholder="Enter your prompt here..."),
|
44 |
+
gr.Dropdown(
|
45 |
+
choices=["English", "Arabic"], # Users choose the language, not the model name
|
46 |
+
label="Choose Language",
|
47 |
+
value="Arabic" # Default to Arabic (ALLAM model)
|
48 |
+
)
|
49 |
],
|
50 |
+
outputs=gr.Textbox(label="Generated Text"),
|
51 |
+
title="Text Generator",
|
52 |
+
description="Enter a prompt and generate text in English or Arabic using AI models.",
|
53 |
+
examples=[
|
54 |
+
["Give me information about Lavender.", "English"],
|
55 |
+
["أعطني معلومات عن نبات اللافندر", "Arabic"]
|
56 |
+
]
|
57 |
)
|
58 |
|
59 |
+
demo.launch()
|
|
|
|