Spaces:
Paused
Paused
File size: 2,596 Bytes
14ba8c7 2b845f2 14ba8c7 83e168c 14ba8c7 |
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 |
import gradio as gr
import anthropic
import base64
import httpx
import os
import time
import random
client = anthropic.Anthropic(
# defaults to os.environ.get("ANTHROPIC_API_KEY")
api_key="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",
)
MODEL_NAME = "claude-3-opus-20240229"
def print_like_dislike(x: gr.LikeData):
print(x.index, x.value, x.liked)
def add_message(history, message):
if message["files"]:
for x in message["files"]:
with open(x, "rb") as image_file:
binary_data = image_file.read()
base_64_encoded_data = base64.b64encode(binary_data)
base64_string = base_64_encoded_data.decode('utf-8')
image_media_type = "image/png"
# message["text"] = "What is present in the uploaded image" if message["text"] == None else message["text"]
message_list = [{
"role": 'user',"content":
[{"type": "image", "source": {"type": "base64", "media_type": image_media_type ,"data": base64_string}},
{"type": "text", "text": message["text"] }
]}]
response = client.messages.create(model=MODEL_NAME, max_tokens=1024, messages=message_list).content[0].text
history.append((x, response))
elif message["text"] is not None:
response = client.messages.create( model=MODEL_NAME, max_tokens=1024,
messages=[ {"role": "user", "content": message["text"]}] ).content[0].text
history.append((message["text"], response))
return history, gr.MultimodalTextbox(value=None, interactive=False)
def bot(history):
response = history[-1][1]
history[-1][1] = ""
for character in response:
history[-1][1] += character
time.sleep(0.01)
yield history
with gr.Blocks() as demo:
chatbot = gr.Chatbot(
[],
elem_id="chatbot",
bubble_full_width=False,
)
chat_input = gr.MultimodalTextbox(interactive=True, file_types=["image"], placeholder="Enter message or upload image file in png ...", show_label=True)
chat_msg = chat_input.submit(add_message, [chatbot, chat_input], [chatbot, chat_input])
bot_msg = chat_msg.then(bot, chatbot, chatbot, api_name="bot_response")
bot_msg.then(lambda: gr.MultimodalTextbox(interactive=True), None, [chat_input])
chatbot.like(print_like_dislike, None, None)
demo.queue()
demo.launch() |