File size: 2,454 Bytes
7abe6e1
ccbcbae
7abe6e1
 
 
 
adbeb82
 
332de37
6b17270
78f7413
 
 
 
 
6b17270
 
7abe6e1
555ef07
 
 
8cd5d91
7abe6e1
 
75e5169
56e913b
ef6a2a0
adbeb82
caa2dce
adbeb82
e207b76
370c03f
ef6a2a0
74524a2
78f7413
74524a2
 
 
f85c8ad
 
75e5169
78f7413
 
 
 
 
 
 
 
13b6f87
74524a2
 
13b6f87
ef6a2a0
74524a2
ef6a2a0
709bce3
13b6f87
ef6a2a0
0330f37
caa2dce
 
 
370c03f
af7cd6b
7abe6e1
5da1cee
caa2dce
dfa34b3
7abe6e1
2d34c43
370c03f
75e5169
a381b61
dfa34b3
2d34c43
7abe6e1
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
import gradio as gr
import boto3, json, os

from dotenv import load_dotenv, find_dotenv
_ = load_dotenv(find_dotenv())

aws_access_key_id = os.environ["AWS_ACCESS_KEY_ID"]
aws_secret_access_key = os.environ["AWS_SECRET_ACCESS_KEY"]

config = {
    "max_tokens": 1000,
    "model": "us.anthropic.claude-3-5-haiku-20241022-v1:0",
    "temperature": 0,
    "top_k": 250,
    "top_p": 0.999,
}

bedrock_runtime = boto3.client(
    aws_access_key_id = aws_access_key_id,
    aws_secret_access_key = aws_secret_access_key,
    service_name = "bedrock-runtime",
    region_name = "us-east-1"
)

def invoke(prompt):
    if not prompt:
        raise gr.Error("Prompt is required.")

    raise gr.Error("Please clone and bring your own credentials.")
    
    completion = ""
    
    try:
        body = {
            "anthropic_version": "bedrock-2023-05-31",
            "messages": [
                {"role": "user", "content": [{"type": "text", "text": prompt}]},
            ],
            "system": "You are a honest, helpful, and harmless bot."
        }
        model_id = config["model"]
        model_kwargs =  { 
            "max_tokens": config["max_tokens"],
            "stop_sequences": ["\n\nHuman"],
            "temperature": config["temperature"],
            "top_k": config["top_k"],
            "top_p": config["top_p"]
        }
        body.update(model_kwargs)
        
        response = bedrock_runtime.invoke_model(modelId=model_id,
                                                body=json.dumps(body))
        
        response_body = json.loads(response.get("body").read())
        completion = response_body.get("content", [])[0].get("text", "")
    except Exception as e:
        completion = e
        
        raise gr.Error(e)

    #print("---")
    #print(completion)
    #print("---")
    
    return completion

description = """<a href='https://www.gradio.app/'>Gradio</a> UI using the <a href='https://aws.amazon.com/bedrock/'>Amazon Bedrock</a> SDK 
                 with <a href='https://www.anthropic.com/'>Anthropic</a> Claude 3.5 Haiku model."""

gr.close_all()

demo = gr.Interface(fn = invoke, 
                    inputs = [gr.Textbox(label = "Prompt", value = "If I dry one shirt in the sun, it takes 1 hour. How long do 3 shirts take?", lines = 1)],
                    outputs = [gr.Markdown(label = "Completion", value = os.environ["COMPLETION"])],
                    description = description)

demo.launch()