File size: 4,103 Bytes
4d26ed5
7efd637
ca8dc25
4c02c40
4d26ed5
 
ca8dc25
4d26ed5
 
e98c6cb
cb0fdde
 
 
 
 
 
 
 
 
d107cdf
4d26ed5
d1e749a
 
 
 
cb0fdde
d1e749a
cb0fdde
 
 
 
 
5975172
cb0fdde
 
 
 
 
 
 
 
 
 
 
 
d1e749a
 
5975172
d1e749a
 
cb0fdde
4d26ed5
d1e749a
4d26ed5
 
cb0fdde
 
6719d1c
cb0fdde
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6719d1c
 
 
 
 
 
 
 
 
cb0fdde
9dc7fb7
6a8b740
6719d1c
4d26ed5
 
 
 
 
 
3807c9a
4d26ed5
cb0fdde
 
74018c7
7bc7ddc
82ee039
4d26ed5
7bc7ddc
 
cb0fdde
 
 
7bc7ddc
 
74018c7
4d26ed5
74018c7
4d26ed5
5e6f5c8
4d26ed5
 
5e6f5c8
4d26ed5
7efd637
4d26ed5
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import os
import gradio as gr
from together import Together
from PIL import Image
import io
import base64

# Initialize the Together AI client
client = Together(api_key=os.environ.get('TOGETHER_API_KEY'))

def encode_image(image_path):
    try:
        with Image.open(image_path) as img:
            buffered = io.BytesIO()
            img.save(buffered, format="PNG")
            return base64.b64encode(buffered.getvalue()).decode('utf-8')
    except Exception as e:
        print(f"Error encoding image: {e}")
        return None

def chat_with_image(message, image, history):
    # Prepare the messages
    messages = [{"role": "system", "content": "You are a helpful assistant that can analyze images and text."}]
    
    for human, assistant in history:
        if human.startswith("Image: "):
            # This is an image message
            image_path = human.split(": ", 1)[1]
            encoded_image = encode_image(image_path)
            if encoded_image:
                messages.append({
                    "role": "user",
                    "content": f"[IMAGE]{encoded_image}[/IMAGE]\nWhat is in this image?"
                })
            else:
                messages.append({"role": "user", "content": "I tried to upload an image, but there was an error."})
        else:
            # This is a text-only message
            messages.append({"role": "user", "content": human})
        messages.append({"role": "assistant", "content": assistant})
    
    # Add the current message
    if image:
        encoded_image = encode_image(image)
        if encoded_image:
            messages.append({
                "role": "user",
                "content": f"[IMAGE]{encoded_image}[/IMAGE]\n{message or 'What is in this image?'}"
            })
        else:
            messages.append({"role": "user", "content": "I tried to upload an image, but there was an error."})
    else:
        messages.append({"role": "user", "content": message})
    
    # Call the Together AI API
    try:
        response = client.chat.completions.create(
            model="meta-llama/Llama-Vision-Free",
            messages=messages,
            max_tokens=512,
            temperature=0.7,
            top_p=0.7,
            top_k=50,
            repetition_penalty=1,
            stop=["<|eot_id|>", "<|eom_id|>"],
            stream=True
        )
        
        # Accumulate the response
        full_response = ""
        for chunk in response:
            if chunk.choices[0].delta.content is not None:
                full_response += chunk.choices[0].delta.content
                yield full_response
    except Exception as e:
        # Enhanced error handling
        import traceback
        traceback.print_exc()
        if hasattr(e, 'response') and e.response is not None:
            try:
                error_content = e.response.json()
                print("Error response JSON:", error_content)
            except Exception:
                print("Error response text:", e.response.text)
        yield f"An error occurred: {str(e)}"



# Create the Gradio interface
with gr.Blocks() as demo:
    chatbot = gr.Chatbot()
    msg = gr.Textbox()
    image = gr.Image(type="filepath")
    clear = gr.Button("Clear")

    def user(user_message, image, history):
        if image:
            return "", None, history + [[f"Image: {image}", None]]
        else:
            return "", None, history + [[user_message, None]]

    def bot(history):
        user_message = history[-1][0]
        image = None
        if user_message.startswith("Image: "):
            image = user_message.split(": ", 1)[1]
            user_message = "What's in this image?"
        
        bot_message = chat_with_image(user_message, image, history[:-1])
        history[-1][1] = ""
        for character in bot_message:
            history[-1][1] += character
            yield history

    msg.submit(user, [msg, image, chatbot], [msg, image, chatbot], queue=False).then(
        bot, chatbot, chatbot
    )
    clear.click(lambda: None, None, chatbot, queue=False)

demo.queue()
demo.launch()