llama4 / app.py
drlau's picture
Update app.py
26bd687 verified
import gradio as gr
from openai import OpenAI
import os
from PIL import Image
import tempfile
# Initialize the OpenAI client with OpenRouter
client = OpenAI(
base_url="https://openrouter.ai/api/v1",
api_key="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",
)
def process_input(text_input, image_input):
messages = [{"role": "user", "content": []}]
# Add text input if provided
if text_input:
messages[0]["content"].append({
"type": "text",
"text": text_input
})
# Add image input if provided
if image_input is not None:
# Save the image to a temporary file
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_file:
image_path = temp_file.name
if isinstance(image_input, str): # If it's already a file path
Image.open(image_input).save(image_path)
else: # If it's an image object from Gradio
image_input.save(image_path)
# Encode and add the image
import base64
with open(image_path, "rb") as image_file:
base64_image = base64.b64encode(image_file.read()).decode('utf-8')
messages[0]["content"].append({
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{base64_image}"
}
})
# Clean up temporary file
os.unlink(image_path)
# If neither text nor image is provided
if not text_input and image_input is None:
return "Please provide either text, an image, or both."
try:
# Call the API
completion = client.chat.completions.create(
extra_body={},
model="meta-llama/llama-4-maverick:free",
messages=messages
)
return completion.choices[0].message.content
except Exception as e:
return f"Error: {str(e)}"
# Create the Gradio interface
with gr.Blocks() as demo:
gr.Markdown("# LLaMA 4 Maverick AI Assistant")
gr.Markdown("Enter text, upload an image, or both to get a response from the AI.")
with gr.Row():
with gr.Column():
text_input = gr.Textbox(label="Text Input", placeholder="Type your question or prompt here...")
image_input = gr.Image(label="Image Input (Optional)", type="pil")
submit_btn = gr.Button("Submit")
with gr.Column():
output = gr.Textbox(label="AI Response", lines=10)
submit_btn.click(
fn=process_input,
inputs=[text_input, image_input],
outputs=output
)
# Launch the app
if __name__ == "__main__":
demo.launch()