File size: 3,042 Bytes
d52c9ce
b3bb424
d52c9ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b3bb424
 
d52c9ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b3bb424
d52c9ce
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
import os
import gradio as gr
from anthropic import Anthropic
from pypdf import PdfReader

# Add the path to your desired knowledge base
reference_document = "" 
reader = PdfReader(reference_document)
text = ''.join(page.extract_text() for page in reader.pages)

# Anthropic API setup
ANTHROPIC_API_KEY = 
os.environ["ANTHROPIC_API_KEY"] = ANTHROPIC_API_KEY

def chat_with_assistant(message, history):
    history_str = "\n".join([f"Human: {h[0]}\nAssistant: {h[1]}" for h in history])
    
    ai_message = f"""You are an AI assistant answering questions based on a reference document. 
    You provide short, clear answers with simple language.
    Use the following text as context for all of your answers:

    {text}

    Previous conversation history:
    {history_str}
    """

    # Add your desired instructions
    
    instructions = """
    
    """ 
    
    system_prompt = f"{ai_message} {instructions}"

    client = Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
    
    response = client.messages.create(
        model="claude-3-5-sonnet-20240620",
      # model="claude-3-haiku-20240307",
        max_tokens=500,
        system=system_prompt,
        messages=[
            {"role": "user", "content": message}
        ]
    )
    
    return response.content[0].text.strip()

# CSS for an Anthropic-looking style
anthropic_theme = gr.themes.Default().set(
    body_background_fill="#FAF9F6",  # Light beige background
    block_background_fill="#FFFFFF",  # White for input blocks
    block_title_text_color="#4A4A4A",  # Dark gray for text
    block_label_background_fill="#F6E3CE",  # Very light orange for labels
    input_background_fill="#FFFFFF",  # White for input fields
    button_primary_background_fill="#D97758",  # Anthropic orange for primary buttons
    button_primary_background_fill_hover="#8A2BE2",  # Darker orange for hover
    button_primary_text_color="#FFFFFF",  # White text on buttons
    button_secondary_background_fill="#F5D0A9",  # Light orange for secondary buttons
    button_secondary_background_fill_hover="#F5D0A9",  # Slightly darker orange for hover
    button_secondary_text_color="#4A4A4A",  # Dark gray text for secondary buttons
    block_border_width="1px",
    block_border_color="#E0E0E0",  # Light gray border
)

# Gradio interface
iface = gr.ChatInterface(
    chat_with_assistant,
    chatbot=gr.Chatbot(height=500),
    textbox=gr.Textbox(placeholder="Type your message here...", container=False, scale=7),

# Change name and description as desired
    title="Claude Custom Assistant",
    description="Chat with an AI assistant powered by Claude 3.5 Sonnet, customs instructions, and a reference document",
    theme=anthropic_theme,
    css=custom_css,

# Change examples as desired
    examples=["What are the key ingredients of a well-planned lesson?", "What might be barriers to learning?", "How does learning happen in the classroom?"],
    cache_examples=True,
    retry_btn=None,
    undo_btn="Delete Previous",
    clear_btn="Clear",
)

iface.launch(share=True)