Update app.py
Browse files
app.py
CHANGED
@@ -5,7 +5,7 @@ from pypdf import PdfReader
|
|
5 |
from sklearn.feature_extraction.text import TfidfVectorizer
|
6 |
from sklearn.metrics.pairwise import cosine_similarity
|
7 |
|
8 |
-
# Set up
|
9 |
ANTHROPIC_API_KEY = os.getenv('ANTHROPIC_API_KEY')
|
10 |
os.environ["ANTHROPIC_API_KEY"] = ANTHROPIC_API_KEY
|
11 |
|
@@ -32,88 +32,114 @@ def get_relevant_chunks(query, chunks, top_n=3):
|
|
32 |
relevant_indices = cosine_similarities.argsort()[-top_n:][::-1]
|
33 |
return [chunks[i] for i in relevant_indices]
|
34 |
|
35 |
-
#
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
def chat_with_assistant(message, history):
|
42 |
# Find relevant chunks based on the user message
|
43 |
relevant_chunks = get_relevant_chunks(message, text_chunks)
|
44 |
context = "\n".join(relevant_chunks)
|
45 |
-
|
46 |
# Prepare the system message
|
47 |
-
system_message = f"""You are
|
48 |
-
|
|
|
|
|
49 |
{context}
|
|
|
50 |
"""
|
51 |
|
52 |
# Customize instructions as needed
|
53 |
-
instructions =
|
54 |
-
You provide useful examples and similes.
|
55 |
-
"""
|
56 |
system_message += instructions
|
57 |
-
|
58 |
# Prepare the message array
|
59 |
messages = []
|
60 |
-
|
61 |
# Add conversation history
|
62 |
for human_msg, ai_msg in history:
|
63 |
messages.append({"role": "user", "content": human_msg})
|
64 |
messages.append({"role": "assistant", "content": ai_msg})
|
65 |
-
|
66 |
# Add the current user message
|
67 |
messages.append({"role": "user", "content": message})
|
68 |
-
|
69 |
# Create Anthropic client
|
70 |
client = Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
|
71 |
-
|
72 |
# Make the API call
|
73 |
response = client.messages.create(
|
74 |
model="claude-3-5-sonnet-20240620",
|
75 |
-
#model="claude-3-haiku-20240307",
|
76 |
max_tokens=500,
|
77 |
system=system_message,
|
78 |
messages=messages
|
79 |
)
|
80 |
-
|
81 |
return response.content[0].text.strip()
|
82 |
|
83 |
-
# CSS for an
|
84 |
-
|
85 |
-
body_background_fill="#
|
86 |
block_background_fill="#FFFFFF", # White for input blocks
|
87 |
-
block_title_text_color="#
|
88 |
-
block_label_background_fill="#
|
89 |
input_background_fill="#FFFFFF", # White for input fields
|
90 |
-
button_primary_background_fill="#
|
91 |
-
button_primary_background_fill_hover="#
|
92 |
button_primary_text_color="#FFFFFF", # White text on buttons
|
93 |
-
button_secondary_background_fill="#
|
94 |
-
button_secondary_background_fill_hover="#
|
95 |
-
button_secondary_text_color="#
|
96 |
block_border_width="1px",
|
97 |
-
block_border_color="#
|
98 |
)
|
99 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
# Gradio interface
|
101 |
iface = gr.ChatInterface(
|
102 |
chat_with_assistant,
|
103 |
-
chatbot=gr.Chatbot(
|
|
|
|
|
|
|
104 |
textbox=gr.Textbox(placeholder="Type your message here...", container=False, scale=7),
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
description="Chat with an AI assistant powered by Claude 3.5 Sonnet, customs instructions, and a reference document",
|
109 |
-
theme=anthropic_theme,
|
110 |
-
|
111 |
-
# Change examples as desired
|
112 |
-
examples=["What are the key principles of instructional design?", "What might be barriers to learning?"],
|
113 |
-
cache_examples=True,
|
114 |
retry_btn=None,
|
115 |
undo_btn="Delete Previous",
|
116 |
clear_btn="Clear",
|
|
|
117 |
)
|
118 |
|
|
|
|
|
|
|
|
|
|
|
|
|
119 |
iface.launch(auth=(username, password))
|
|
|
5 |
from sklearn.feature_extraction.text import TfidfVectorizer
|
6 |
from sklearn.metrics.pairwise import cosine_similarity
|
7 |
|
8 |
+
# Set up Anthropic API key in HF secrets
|
9 |
ANTHROPIC_API_KEY = os.getenv('ANTHROPIC_API_KEY')
|
10 |
os.environ["ANTHROPIC_API_KEY"] = ANTHROPIC_API_KEY
|
11 |
|
|
|
32 |
relevant_indices = cosine_similarities.argsort()[-top_n:][::-1]
|
33 |
return [chunks[i] for i in relevant_indices]
|
34 |
|
35 |
+
# Function to process multiple PDFs
|
36 |
+
def process_pdfs(pdf_files):
|
37 |
+
all_chunks = []
|
38 |
+
for pdf_file in pdf_files:
|
39 |
+
reader = PdfReader(pdf_file)
|
40 |
+
full_text = ''.join(page.extract_text() for page in reader.pages)
|
41 |
+
chunks = chunk_text(full_text)
|
42 |
+
all_chunks.extend(chunks)
|
43 |
+
return all_chunks
|
44 |
+
|
45 |
+
# Add the paths to your desired knowledge base PDFs
|
46 |
+
reference_documents = ["Rosenshine+Principles.pdf", AnotherDocument.pdf]
|
47 |
+
text_chunks = process_pdfs(reference_documents)
|
48 |
+
|
49 |
+
instructions = os.getenv('INSTRUCTIONS')
|
50 |
def chat_with_assistant(message, history):
|
51 |
# Find relevant chunks based on the user message
|
52 |
relevant_chunks = get_relevant_chunks(message, text_chunks)
|
53 |
context = "\n".join(relevant_chunks)
|
54 |
+
|
55 |
# Prepare the system message
|
56 |
+
system_message = f"""You are an impersonator and an educator.
|
57 |
+
Your role is to adopt the personality, style, psychology, ideas, backgroundm and circumstances of a historical figure during a chat with students.
|
58 |
+
Your goal is to help them understand the historical figure better through and engaging discussion.
|
59 |
+
Use the following as context for your answers.
|
60 |
{context}
|
61 |
+
However, use it seamlessly as background knowledge for a lively discussion. Do not provide citations or adopt a Q&A or academic tone.
|
62 |
"""
|
63 |
|
64 |
# Customize instructions as needed
|
65 |
+
instructions = instructions
|
|
|
|
|
66 |
system_message += instructions
|
67 |
+
|
68 |
# Prepare the message array
|
69 |
messages = []
|
70 |
+
|
71 |
# Add conversation history
|
72 |
for human_msg, ai_msg in history:
|
73 |
messages.append({"role": "user", "content": human_msg})
|
74 |
messages.append({"role": "assistant", "content": ai_msg})
|
75 |
+
|
76 |
# Add the current user message
|
77 |
messages.append({"role": "user", "content": message})
|
78 |
+
|
79 |
# Create Anthropic client
|
80 |
client = Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
|
81 |
+
|
82 |
# Make the API call
|
83 |
response = client.messages.create(
|
84 |
model="claude-3-5-sonnet-20240620",
|
|
|
85 |
max_tokens=500,
|
86 |
system=system_message,
|
87 |
messages=messages
|
88 |
)
|
89 |
+
|
90 |
return response.content[0].text.strip()
|
91 |
|
92 |
+
# CSS for an ISP-looking style
|
93 |
+
isp_theme = gr.themes.Default().set(
|
94 |
+
body_background_fill="#F0F8FF", # Light blue background
|
95 |
block_background_fill="#FFFFFF", # White for input blocks
|
96 |
+
block_title_text_color="#00008B", # Dark blue for text
|
97 |
+
block_label_background_fill="#FFD700", # Yellow for labels
|
98 |
input_background_fill="#FFFFFF", # White for input fields
|
99 |
+
button_primary_background_fill="#FF1493", # Pink/Magenta for primary buttons
|
100 |
+
button_primary_background_fill_hover="#FF69B4", # Lighter pink for hover
|
101 |
button_primary_text_color="#FFFFFF", # White text on buttons
|
102 |
+
button_secondary_background_fill="#FFD700", # Yellow for secondary buttons
|
103 |
+
button_secondary_background_fill_hover="#FFA500", # Orange for hover
|
104 |
+
button_secondary_text_color="#00008B", # Dark blue text for secondary buttons
|
105 |
block_border_width="1px",
|
106 |
+
block_border_color="#00008B", # Dark blue border
|
107 |
)
|
108 |
|
109 |
+
# Custom CSS for logo positioning
|
110 |
+
custom_css = """
|
111 |
+
#logo-img {
|
112 |
+
position: absolute;
|
113 |
+
top: 20px;
|
114 |
+
right: 20px;
|
115 |
+
width: 100px;
|
116 |
+
height: auto;
|
117 |
+
}
|
118 |
+
"""
|
119 |
+
|
120 |
+
assistant_avatar = os.getenv('AVATAR')
|
121 |
+
assistant_title = os.getenv('TITLE')
|
122 |
# Gradio interface
|
123 |
iface = gr.ChatInterface(
|
124 |
chat_with_assistant,
|
125 |
+
chatbot=gr.Chatbot(
|
126 |
+
height=500,
|
127 |
+
avatar_images=(None, assistant_avatar)
|
128 |
+
),
|
129 |
textbox=gr.Textbox(placeholder="Type your message here...", container=False, scale=7),
|
130 |
+
title=assistant_title,
|
131 |
+
description="Chat with a Historical Figure",
|
132 |
+
theme=isp_theme,
|
|
|
|
|
|
|
|
|
|
|
|
|
133 |
retry_btn=None,
|
134 |
undo_btn="Delete Previous",
|
135 |
clear_btn="Clear",
|
136 |
+
css=custom_css,
|
137 |
)
|
138 |
|
139 |
+
assistant_logo = os.getenv('LOGO')
|
140 |
+
|
141 |
+
# Add logo to the interface
|
142 |
+
logo_html = '<img id="logo-img" src=assistant_logo alt="Assistant Logo">'
|
143 |
+
iface.attachments.append(logo_html)
|
144 |
+
|
145 |
iface.launch(auth=(username, password))
|