drlau commited on
Commit
26bd687
·
verified ·
1 Parent(s): 239bf24

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -24
app.py CHANGED
@@ -1,29 +1,86 @@
 
1
  from openai import OpenAI
 
 
 
2
 
 
3
  client = OpenAI(
4
- base_url="https://openrouter.ai/api/v1",
5
- api_key="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",
6
  )
7
 
8
- completion = client.chat.completions.create(
9
- extra_body={},
10
- model="meta-llama/llama-4-maverick:free",
11
- messages=[
12
- {
13
- "role": "user",
14
- "content": [
15
- {
16
- "type": "text",
17
- "text": "What is in this image?"
18
- },
19
- {
20
- "type": "image_url",
21
- "image_url": {
22
- "url": "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg"
23
- }
24
- }
25
- ]
26
- }
27
- ]
28
- )
29
- print(completion.choices[0].message.content)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
  from openai import OpenAI
3
+ import os
4
+ from PIL import Image
5
+ import tempfile
6
 
7
+ # Initialize the OpenAI client with OpenRouter
8
  client = OpenAI(
9
+ base_url="https://openrouter.ai/api/v1",
10
+ api_key="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",
11
  )
12
 
13
+ def process_input(text_input, image_input):
14
+ messages = [{"role": "user", "content": []}]
15
+
16
+ # Add text input if provided
17
+ if text_input:
18
+ messages[0]["content"].append({
19
+ "type": "text",
20
+ "text": text_input
21
+ })
22
+
23
+ # Add image input if provided
24
+ if image_input is not None:
25
+ # Save the image to a temporary file
26
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_file:
27
+ image_path = temp_file.name
28
+ if isinstance(image_input, str): # If it's already a file path
29
+ Image.open(image_input).save(image_path)
30
+ else: # If it's an image object from Gradio
31
+ image_input.save(image_path)
32
+
33
+ # Encode and add the image
34
+ import base64
35
+ with open(image_path, "rb") as image_file:
36
+ base64_image = base64.b64encode(image_file.read()).decode('utf-8')
37
+
38
+ messages[0]["content"].append({
39
+ "type": "image_url",
40
+ "image_url": {
41
+ "url": f"data:image/jpeg;base64,{base64_image}"
42
+ }
43
+ })
44
+
45
+ # Clean up temporary file
46
+ os.unlink(image_path)
47
+
48
+ # If neither text nor image is provided
49
+ if not text_input and image_input is None:
50
+ return "Please provide either text, an image, or both."
51
+
52
+ try:
53
+ # Call the API
54
+ completion = client.chat.completions.create(
55
+ extra_body={},
56
+ model="meta-llama/llama-4-maverick:free",
57
+ messages=messages
58
+ )
59
+
60
+ return completion.choices[0].message.content
61
+ except Exception as e:
62
+ return f"Error: {str(e)}"
63
+
64
+ # Create the Gradio interface
65
+ with gr.Blocks() as demo:
66
+ gr.Markdown("# LLaMA 4 Maverick AI Assistant")
67
+ gr.Markdown("Enter text, upload an image, or both to get a response from the AI.")
68
+
69
+ with gr.Row():
70
+ with gr.Column():
71
+ text_input = gr.Textbox(label="Text Input", placeholder="Type your question or prompt here...")
72
+ image_input = gr.Image(label="Image Input (Optional)", type="pil")
73
+ submit_btn = gr.Button("Submit")
74
+
75
+ with gr.Column():
76
+ output = gr.Textbox(label="AI Response", lines=10)
77
+
78
+ submit_btn.click(
79
+ fn=process_input,
80
+ inputs=[text_input, image_input],
81
+ outputs=output
82
+ )
83
+
84
+ # Launch the app
85
+ if __name__ == "__main__":
86
+ demo.launch()