jeremierostan commited on
Commit
84e620c
·
verified ·
1 Parent(s): 1607e5b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -52
app.py CHANGED
@@ -1,63 +1,85 @@
 
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
-
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
 
 
 
 
9
 
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
19
 
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
-
26
- messages.append({"role": "user", "content": message})
 
 
 
27
 
28
- response = ""
 
29
 
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
 
39
- response += token
40
- yield response
 
 
 
 
 
 
41
 
42
  """
43
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
44
- """
45
- demo = gr.ChatInterface(
46
- respond,
47
- additional_inputs=[
48
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
49
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
50
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
51
- gr.Slider(
52
- minimum=0.1,
53
- maximum=1.0,
54
- value=0.95,
55
- step=0.05,
56
- label="Top-p (nucleus sampling)",
57
- ),
58
- ],
59
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
 
 
 
 
 
 
 
 
61
 
62
- if __name__ == "__main__":
63
- demo.launch()
 
1
+ import os
2
  import gradio as gr
3
+ from anthropic import Anthropic
 
 
 
 
 
4
 
5
+ # Set up Anthropic API key
6
+ ANTHROPIC_API_KEY = os.getenv('ANTHROPIC_API_KEY')
7
+ os.environ["ANTHROPIC_API_KEY"] = ANTHROPIC_API_KEY
8
 
9
+ def chat_with_assistant(message, history):
10
+ # Prepare the system message
11
+ system_message = """
 
 
 
 
 
 
12
 
13
+ #Context:
14
+ You are an instructional coach. Instructional coaches help educators reflect on their professional practice, get an accurate picture of reality, find the sources of problems, devise potential solutions, and make plans to reach specific goals.
15
+ To do so, ask the following 6 questions:
16
+ -“How happy are you with the way things are going?” [This helps people assess their situation, performance, and needs.]
17
+ -“What would it look like if they went (even) better?”  [This helps people envision and set an improvement goal.]
18
+ -“Why might this be happening / not happening?” [This helps people find the potential sources of problems.]
19
+ -“What can you try to make this happen / not happen?” [This helps people brainstorm solutions.]
20
+ -“How do you want to move forward?” [This helps people design concrete action plans to implement solutions, measure progress, and reach their goals.]
21
+ -”Where can you find the assistance you need?” [This helps people feel supported and identify resources in their environment.]
22
+ -At the end of the exchange, invite the user to circle back and get back to you about their observations and results for a follow-up discussion.
23
 
24
+ #Objective:
25
+ -Acting as a coach, ask the previous 6 questions to help me think deeper and improve continuously as an educator, all while making me feel confident.
26
 
27
+ #Instructions:
28
+ Make sure to do the following:
29
+ -Paraphrase, summarize, generalize, organize my responses, and ask additional clarifying questions as necessary.
30
+ -Ask if you can offer suggestions when it seems necessary.
31
+ -Ask if I want to use strategies leveraging AI capabilities and tools, including how you can help as a generative AI chatbot.
32
+ -Always use positive and supportive language.
33
+ -Ask each question separately, building on my answers.
 
34
 
35
+ #Important:
36
+ -Do not ask all questions at once. Instead, wait for my response to question n before asking question n+1.
37
+ -The flow of the chat should be ({…})
38
+ {You ask.
39
+ I respond.
40
+ You build on my response to ask the next question.
41
+ I respond.
42
+ Etc.}
43
 
44
  """
45
+
46
+ # Prepare the message array
47
+ messages = []
48
+
49
+ # Add conversation history
50
+ for human_msg, ai_msg in history:
51
+ messages.append({"role": "user", "content": human_msg})
52
+ messages.append({"role": "assistant", "content": ai_msg})
53
+
54
+ # Add the current user message
55
+ messages.append({"role": "user", "content": message})
56
+
57
+ # Create Anthropic client
58
+ client = Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
59
+
60
+ # Make the API call
61
+ response = client.messages.create(
62
+ model="claude-3-5-sonnet-20240620",
63
+ max_tokens=333,
64
+ system=system_message,
65
+ messages=messages,
66
+ )
67
+
68
+ # Return the assistant's response
69
+ return response.content[0].text.strip()
70
+
71
+ # Gradio interface
72
+ iface = gr.ChatInterface(
73
+ chat_with_assistant,
74
+ chatbot=gr.Chatbot(height=500),
75
+ textbox=gr.Textbox(placeholder="Type your message here...", container=False, scale=7),
76
 
77
+ # Styling
78
+ title="🧑‍🏫 ED Coach",
79
+ description="Hi! I'm Ed, your virtual instructional coach",
80
+ retry_btn=None,
81
+ undo_btn="Delete Previous",
82
+ clear_btn="Clear",
83
+ )
84
 
85
+ iface.launch()