import gradio as gr from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline import torch # Model name (you can change to a larger Granite model if GPU is available) MODEL_NAME = "ibm-granite/granite-3b-instruct" # Load tokenizer and model tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME) model = AutoModelForCausalLM.from_pretrained( MODEL_NAME, torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32, device_map="auto" ) # Create text-generation pipeline generator = pipeline( "text-generation", model=model, tokenizer=tokenizer ) def generate_response(prompt): if not prompt.strip(): return "Please enter a prompt." output = generator( prompt, max_new_tokens=300, do_sample=True, temperature=0.7, top_p=0.9 ) return output[0]["generated_text"] # Gradio UI demo = gr.Interface( fn=generate_response, inputs=gr.Textbox( lines=6, placeholder="Ask something...", label="User Prompt" ), outputs=gr.Textbox( lines=10, label="Granite Response" ), title="IBM Granite Model – Gradio Demo", description="A simple Hugging Face Space using IBM Granite with Gradio" ) if __name__ == "__main__": demo.launch()