| import gradio as gr |
| from openai import OpenAI |
| import os |
| from PIL import Image |
| import tempfile |
|
|
| |
| 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": []}] |
|
|
| |
| if text_input: |
| messages[0]["content"].append({ |
| "type": "text", |
| "text": text_input |
| }) |
|
|
| |
| if image_input is not None: |
| |
| with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_file: |
| image_path = temp_file.name |
| if isinstance(image_input, str): |
| Image.open(image_input).save(image_path) |
| else: |
| image_input.save(image_path) |
|
|
| |
| 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}" |
| } |
| }) |
|
|
| |
| os.unlink(image_path) |
|
|
| |
| if not text_input and image_input is None: |
| return "Please provide either text, an image, or both." |
|
|
| try: |
| |
| 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)}" |
|
|
| |
| 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 |
| ) |
|
|
| |
| if __name__ == "__main__": |
| demo.launch() |