File size: 1,644 Bytes
a982432
 
 
f7ffb56
a982432
e4baaed
a982432
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e4baaed
a982432
 
 
f7ffb56
0765a71
cb16315
 
 
a982432
 
 
 
7a74a6d
a982432
cb16315
e4baaed
a982432
e4baaed
 
 
a982432
cb16315
 
a982432
 
 
 
8c3215b
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
import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
import torch
import spaces

# Load models for English and Arabic
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=True  # Changed to enable sampling for more creative outputs
    )
    return generator

@spaces.GPU
def generate_text(model_name, prompt):
    generator = load_model(model_name)
    messages = [{"role": "user", "content": prompt}]
    output = generator(messages)
    return output[0]["generated_text"]

# Create Gradio interface
demo = gr.Interface(
    fn=generate_text,
    inputs=[
        gr.Radio(choices=["microsoft/Phi-3-mini-4k-instruct", "ALLaM-AI/ALLaM-7B-Instruct-preview"], label="Select Language"),
        gr.Textbox(lines=2, placeholder="Enter your story prompt here...")
    ],
    outputs=gr.Textbox(label="Generated Story"),
    title="Kids Storyteller",
    description="Choose a language and enter a prompt to generate a fun story for kids!",
    examples=[
        ["microsoft/Phi-3-mini-4k-instruct", "Once upon a time in a magical forest..."],
        ["ALLaM-AI/ALLaM-7B-Instruct-preview", "في قديم الزمان في غابة سحرية..."]
    ]
)

demo.launch()