Update app.py
Browse files
app.py
CHANGED
@@ -1,63 +1,87 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
-
from
|
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 |
-
demo.launch()
|
|
|
1 |
+
import os
|
2 |
import gradio as gr
|
3 |
+
from anthropic import Anthropic
|
4 |
+
from pypdf import PdfReader
|
5 |
+
|
6 |
+
# Add the path to your desired knowledge base
|
7 |
+
reference_document = ""
|
8 |
+
reader = PdfReader(reference_document)
|
9 |
+
text = ''.join(page.extract_text() for page in reader.pages)
|
10 |
+
|
11 |
+
# Anthropic API setup
|
12 |
+
ANTHROPIC_API_KEY =
|
13 |
+
os.environ["ANTHROPIC_API_KEY"] = ANTHROPIC_API_KEY
|
14 |
+
|
15 |
+
def chat_with_assistant(message, history):
|
16 |
+
history_str = "\n".join([f"Human: {h[0]}\nAssistant: {h[1]}" for h in history])
|
17 |
+
|
18 |
+
ai_message = f"""You are an AI assistant answering questions based on a reference document.
|
19 |
+
You provide short, clear answers with simple language.
|
20 |
+
Use the following text as context for all of your answers:
|
21 |
+
|
22 |
+
{text}
|
23 |
+
|
24 |
+
Previous conversation history:
|
25 |
+
{history_str}
|
26 |
+
"""
|
27 |
+
|
28 |
+
# Add your desired instructions
|
29 |
+
|
30 |
+
instructions = """
|
31 |
+
|
32 |
+
"""
|
33 |
+
|
34 |
+
system_prompt = f"{ai_message} {instructions}"
|
35 |
+
|
36 |
+
client = Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
|
37 |
+
|
38 |
+
response = client.messages.create(
|
39 |
+
model="claude-3-5-sonnet-20240620",
|
40 |
+
# model="claude-3-haiku-20240307",
|
41 |
+
max_tokens=500,
|
42 |
+
system=system_prompt,
|
43 |
+
messages=[
|
44 |
+
{"role": "user", "content": message}
|
45 |
+
]
|
46 |
+
)
|
47 |
+
|
48 |
+
return response.content[0].text.strip()
|
49 |
+
|
50 |
+
# CSS for an Anthropic-looking style
|
51 |
+
anthropic_theme = gr.themes.Default().set(
|
52 |
+
body_background_fill="#FAF9F6", # Light beige background
|
53 |
+
block_background_fill="#FFFFFF", # White for input blocks
|
54 |
+
block_title_text_color="#4A4A4A", # Dark gray for text
|
55 |
+
block_label_background_fill="#F6E3CE", # Very light orange for labels
|
56 |
+
input_background_fill="#FFFFFF", # White for input fields
|
57 |
+
button_primary_background_fill="#D97758", # Anthropic orange for primary buttons
|
58 |
+
button_primary_background_fill_hover="#8A2BE2", # Darker orange for hover
|
59 |
+
button_primary_text_color="#FFFFFF", # White text on buttons
|
60 |
+
button_secondary_background_fill="#F5D0A9", # Light orange for secondary buttons
|
61 |
+
button_secondary_background_fill_hover="#F5D0A9", # Slightly darker orange for hover
|
62 |
+
button_secondary_text_color="#4A4A4A", # Dark gray text for secondary buttons
|
63 |
+
block_border_width="1px",
|
64 |
+
block_border_color="#E0E0E0", # Light gray border
|
65 |
)
|
66 |
|
67 |
+
# Gradio interface
|
68 |
+
iface = gr.ChatInterface(
|
69 |
+
chat_with_assistant,
|
70 |
+
chatbot=gr.Chatbot(height=500),
|
71 |
+
textbox=gr.Textbox(placeholder="Type your message here...", container=False, scale=7),
|
72 |
+
|
73 |
+
# Change name and description as desired
|
74 |
+
title="Claude Custom Assistant",
|
75 |
+
description="Chat with an AI assistant powered by Claude 3.5 Sonnet, customs instructions, and a reference document",
|
76 |
+
theme=anthropic_theme,
|
77 |
+
css=custom_css,
|
78 |
+
|
79 |
+
# Change examples as desired
|
80 |
+
examples=["What are the key ingredients of a well-planned lesson?", "What might be barriers to learning?", "How does learning happen in the classroom?"],
|
81 |
+
cache_examples=True,
|
82 |
+
retry_btn=None,
|
83 |
+
undo_btn="Delete Previous",
|
84 |
+
clear_btn="Clear",
|
85 |
+
)
|
86 |
|
87 |
+
iface.launch(share=True)
|
|