Spaces:
Runtime error
Runtime error
Update app.py
Browse files1234567import gradio as grdef greet(name):return "Hello " + name + "!!"demo = gr.Interface(fn=greet, inputs="text", outputs="text")demo.launch()12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879# import gradio as gr# def greet(name):# return "Hello " + name + "!!"# demo = gr.Interface(fn=greet, inputs="text", outputs="text")# demo.launch()import gradio as grimport requestsimport torchfrom PIL import Imagefrom transformers import MllamaForConditionalGeneration, AutoProcessor# Load the Llama 3.2 Vision Modeldef load_llama_model():model_id = "meta-llama/Llama-3.2-11B-Vision"# Load model and processormodel = MllamaForConditionalGeneration.from_pretrained(model_id,torch_dtype=torch.bfloat16,device_map="auto",)processor = AutoProcessor.from_pretrained(model_id)return model, processor# Function to generate predictions for text and imagedef process_input(text, image=None):model, processor = load_llama_model()if image:# If an image is uploaded, process it as a PIL Image objectvision_input = image.convert("RGB").resize((224, 224))prompt = f"<|image|><|begin_of_text|>{text}"# Process image and text togetherinputs = processor(vision_input, prompt, return_tensors="pt").to(model.device)else:# If no image is uploaded, just process the textprompt = f"<|begin_of_text|>{text}"inputs = processor(prompt, return_tensors="pt").to(model.device)# Generate output from the modeloutputs = model.generate(**inputs, max_new_tokens=100)# Decode the output to return a readable textdecoded_output = processor.decode(outputs[0], skip_special_tokens=True)return decoded_output# Gradio Interface Setupdef demo():# Define Gradio input and output componentstext_input = gr.Textbox(label="Text Input", placeholder="Enter text here", lines=5)# Use type="pil" to work with PIL Image objectsimage_input = gr.Image(label="Upload an Image", type="pil")output = gr.Textbox(label="Model Output", lines=5)# Define the interface layoutinterface = gr.Interface(fn=process_input,inputs=[text_input, image_input],outputs=output,title="Llama 3.2 Multimodal Text-Image Analyzer",description="Upload an image and/or provide text for analysis using the Llama 3.2 Vision Model.")# Launch the demointerface.launch()# Run the demoif __name__ == "__main__":demo()
app.py+76-4