jeremierostan commited on
Commit
d52c9ce
1 Parent(s): 1934a02

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +83 -59
app.py CHANGED
@@ -1,63 +1,87 @@
 
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
+ 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)