import os import gradio as gr from anthropic import Anthropic from pypdf import PdfReader # Set up username and password username = "Username" password = "Password" # Add the path to your desired knowledge base reference_document = "The Learning Cycle.pdf" reader = PdfReader(reference_document) text = ''.join(page.extract_text() for page in reader.pages) # Anthropic API setup ANTHROPIC_API_KEY = os.getenv('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, # 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(auth=(username, password))