jeremierostan commited on
Commit
3f3f2a3
·
verified ·
1 Parent(s): 8294e01

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -36
app.py CHANGED
@@ -1,12 +1,23 @@
1
- import gradio as gr
2
- import anthropic
3
  import os
 
 
4
 
5
- # Initialize the Anthropic client
6
- client = anthropic.Anthropic(api_key=os.environ.get("ANTHROPIC_API_KEY"))
 
7
 
8
- # System message
9
- SYSTEM_MESSAGE = """
 
 
 
 
 
 
 
 
 
10
  #Context
11
  -You are an instructional designer with an expertise in Understanding by Design
12
  -Help me create a unit plan by following the instructions below
@@ -61,44 +72,68 @@ This MUST include an assessment plan (= series of formative and summative assess
61
  ***
62
  Unit plan:
63
  """
64
-
65
- # Function to generate response using Claude
66
- def generate_response(messages):
 
 
 
 
 
 
 
 
 
 
 
 
 
67
  response = client.messages.create(
68
  model="claude-3-5-sonnet-20240620",
69
  max_tokens=1500,
70
- temperature=0.5,
71
- system=SYSTEM_MESSAGE,
72
  messages=messages
73
  )
74
- return response.content
75
-
76
- # Gradio chat interface
77
- def chat_interface(message, history):
78
- messages = [{"role": "user" if i % 2 == 0 else "assistant", "content": m} for i, m in enumerate(sum(history, []))]
79
 
80
- messages.append({"role": "user", "content": message})
81
-
82
- response = generate_response(messages)
83
- return response
84
 
85
- # Create a Gradio interface
86
- with gr.Blocks() as iface:
87
- chatbot = gr.Chatbot()
88
- msg = gr.Textbox()
89
- clear = gr.Button("Clear")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90
 
91
- def user(user_message, history):
92
- return "", history + [[user_message, None]]
 
 
 
93
 
94
- def bot(history):
95
- bot_message = chat_interface(history[-1][0], history[:-1])
96
- history[-1][1] = bot_message
97
- return history
98
 
99
- msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
100
- bot, [chatbot], [chatbot]
101
- )
102
- clear.click(lambda: None, None, chatbot, queue=False)
 
103
 
104
- iface.launch()
 
1
+ import time
 
2
  import os
3
+ import gradio as gr
4
+ from anthropic import Anthropic
5
 
6
+ # Set up your Anthropic API key in HF secrets
7
+ ANTHROPIC_API_KEY = os.getenv('ANTHROPIC_API_KEY')
8
+ os.environ["ANTHROPIC_API_KEY"] = ANTHROPIC_API_KEY
9
 
10
+ # Set up username and password in HF secrets
11
+ username = os.getenv('username')
12
+ password = os.getenv('password')
13
+
14
+ def chat_with_assistant(message, history):
15
+ # Find relevant chunks based on the user message
16
+ relevant_chunks = get_relevant_chunks(message, text_chunks)
17
+ context = "\n".join(relevant_chunks)
18
+
19
+ # Prepare the system message
20
+ system_message = """
21
  #Context
22
  -You are an instructional designer with an expertise in Understanding by Design
23
  -Help me create a unit plan by following the instructions below
 
72
  ***
73
  Unit plan:
74
  """
75
+
76
+ # Prepare the message array
77
+ messages = []
78
+
79
+ # Add conversation history
80
+ for human_msg, ai_msg in history:
81
+ messages.append({"role": "user", "content": human_msg})
82
+ messages.append({"role": "assistant", "content": ai_msg})
83
+
84
+ # Add the current user message
85
+ messages.append({"role": "user", "content": message})
86
+
87
+ # Create Anthropic client
88
+ client = Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
89
+
90
+ # Make the API call
91
  response = client.messages.create(
92
  model="claude-3-5-sonnet-20240620",
93
  max_tokens=1500,
94
+ system=system_message,
 
95
  messages=messages
96
  )
 
 
 
 
 
97
 
98
+ return response.content[0].text.strip()
 
 
 
99
 
100
+ def slow_echo(message, history):
101
+ for i in range(len(message)):
102
+ time.sleep(0.3)
103
+ yield "You typed: " + message[: i+1]
104
+
105
+ # CSS for an Anthropic-looking style
106
+ anthropic_theme = gr.themes.Default().set(
107
+ body_background_fill="#FAF9F6", # Light beige background
108
+ block_background_fill="#FFFFFF", # White for input blocks
109
+ block_title_text_color="#4A4A4A", # Dark gray for text
110
+ block_label_background_fill="#F6E3CE", # Very light orange for labels
111
+ input_background_fill="#FFFFFF", # White for input fields
112
+ button_primary_background_fill="#D97758", # Anthropic orange for primary buttons
113
+ button_primary_background_fill_hover="#8A2BE2", # Darker orange for hover
114
+ button_primary_text_color="#FFFFFF", # White text on buttons
115
+ button_secondary_background_fill="#F5D0A9", # Light orange for secondary buttons
116
+ button_secondary_background_fill_hover="#F5D0A9", # Slightly darker orange for hover
117
+ button_secondary_text_color="#4A4A4A", # Dark gray text for secondary buttons
118
+ block_border_width="1px",
119
+ block_border_color="#E0E0E0", # Light gray border
120
+ )
121
 
122
+ # Gradio interface
123
+ iface = gr.ChatInterface(
124
+ chat_with_assistant,
125
+ chatbot=gr.Chatbot(height=500),
126
+ textbox=gr.Textbox(placeholder="Type your message here...", container=False, scale=7),
127
 
128
+ # Change name and description as desired
129
+ title="Unit Planner",
130
+ description="I help you plan units aligned with UbD and our SEQTA template. Please share the desired subject, grade level, and learning objectives.",
131
+ theme=anthropic_theme,
132
 
133
+ # Change examples as desired
134
+ retry_btn=None,
135
+ undo_btn="Delete Previous",
136
+ clear_btn="Clear",
137
+ )
138
 
139
+ iface(slow_echo).launch