DemahAlmutairi's picture
Update app.py
cb16315 verified
raw
history blame
1.65 kB
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(prompt, model_name):
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_story,
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()