|
import os |
|
import gradio as gr |
|
from anthropic import Anthropic |
|
from pypdf import PdfReader |
|
|
|
|
|
username = os.getenv('username') |
|
password = os.getenv('password') |
|
|
|
|
|
reference_document = "The Learning Cycle.pdf" |
|
reader = PdfReader(reference_document) |
|
text = ''.join(page.extract_text() for page in reader.pages) |
|
|
|
|
|
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} |
|
""" |
|
|
|
|
|
|
|
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", |
|
|
|
max_tokens=500, |
|
system=system_prompt, |
|
messages=[ |
|
{"role": "user", "content": message} |
|
] |
|
) |
|
|
|
return response.content[0].text.strip() |
|
|
|
|
|
anthropic_theme = gr.themes.Default().set( |
|
body_background_fill="#FAF9F6", |
|
block_background_fill="#FFFFFF", |
|
block_title_text_color="#4A4A4A", |
|
block_label_background_fill="#F6E3CE", |
|
input_background_fill="#FFFFFF", |
|
button_primary_background_fill="#D97758", |
|
button_primary_background_fill_hover="#8A2BE2", |
|
button_primary_text_color="#FFFFFF", |
|
button_secondary_background_fill="#F5D0A9", |
|
button_secondary_background_fill_hover="#F5D0A9", |
|
button_secondary_text_color="#4A4A4A", |
|
block_border_width="1px", |
|
block_border_color="#E0E0E0", |
|
) |
|
|
|
|
|
iface = gr.ChatInterface( |
|
chat_with_assistant, |
|
chatbot=gr.Chatbot(height=500), |
|
textbox=gr.Textbox(placeholder="Type your message here...", container=False, scale=7), |
|
|
|
|
|
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, |
|
|
|
|
|
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)) |