File size: 3,166 Bytes
12377a0
 
 
 
 
99bf49f
 
 
 
 
 
12377a0
99bf49f
 
 
 
 
 
 
 
 
 
 
 
 
12377a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99bf49f
12377a0
 
 
 
 
 
 
 
 
 
99bf49f
12377a0
 
 
 
 
 
99bf49f
12377a0
 
99bf49f
12377a0
 
 
 
 
99bf49f
8d4949b
 
99bf49f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12377a0
 
 
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
import gradio as gr
import anthropic
from anthropic.types import ContentBlock
from time import time

def claude_conversation(message, history, api_key, uploaded_file):
    if not api_key:
        return "Please enter your Anthropic API key."

    if uploaded_file is None:
        return "Please upload a text document."

    client = anthropic.Anthropic(api_key=api_key)
    
    # Read the uploaded file content
    try:
        if hasattr(uploaded_file, 'read'):
            text = uploaded_file.read().decode('utf-8')
        else:
            # If it's already a string (filename), we need to open and read the file
            with open(uploaded_file, 'r') as file:
                text = file.read()
    except Exception as e:
        return f"Error reading file: {str(e)}"
    
    # Prepare the conversation history
    messages = []
    for human, assistant in history:
        if human.strip():  # Only add non-empty human messages
            messages.append({"role": "user", "content": human})
        if assistant.strip():  # Only add non-empty assistant messages
            messages.append({"role": "assistant", "content": assistant})
    
    # Add the new message if it's not empty
    if message.strip():
        messages.append({"role": "user", "content": message})
    else:
        return "Please enter a non-empty message."

    try:
        # Make the API call
        start = time()
        response = client.beta.prompt_caching.messages.create(
            model="claude-3-5-sonnet-20240620",
            max_tokens=1024,
            system=[
                {
                    "type": "text",
                    "text": "You are an AI assistant tasked with analyzing legal documents."
                },
                {
                    "type": "text",
                    "text": f"Document content:\n\n{text}",
                    "cache_control": {"type": "ephemeral"}
                }
            ],
            messages=messages
        )

        end = time()
        print(f"Elapsed time: {end - start} seconds")

        print (response)
        return response.content[0].text
    except anthropic.APIError as e:
        return f"An error occurred: {str(e)}"

# Create the Gradio interface
with gr.Blocks() as demo:
    gr.Markdown("# Claude Prompt Caching Demo")
    gr.Markdown("Upload a lengthy document and ask questions about it.")
    
    api_key_input = gr.Textbox(label="Enter your Anthropic API key", type="password")
    file_upload = gr.File(label="Upload text document", file_types=[".txt"])
    
    chatbot = gr.Chatbot()
    msg = gr.Textbox()
    submit_button = gr.Button("Submit")
    clear = gr.ClearButton([msg, chatbot])

    def respond(message, chat_history, api_key, uploaded_file):
        bot_message = claude_conversation(message, chat_history, api_key, uploaded_file)
        chat_history.append((message, bot_message))
        return "", chat_history

    submit_button.click(respond, inputs=[msg, chatbot, api_key_input, file_upload], outputs=[msg, chatbot])
    msg.submit(respond, inputs=[msg, chatbot, api_key_input, file_upload], outputs=[msg, chatbot])

# Launch the app
demo.launch()