Trinoid commited on
Commit
16b9df4
·
verified ·
1 Parent(s): 5e327fc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -52
app.py CHANGED
@@ -1,24 +1,11 @@
1
  import gradio as gr
2
- import os
3
- import torch
4
- from transformers import AutoModelForCausalLM, AutoTokenizer
5
 
6
  """
7
- Load model and tokenizer directly using transformers
8
  """
9
- model_name = "PlantWisdom/Data_Management_Mistral"
10
 
11
- # Load tokenizer and model
12
- print("Loading tokenizer...")
13
- tokenizer = AutoTokenizer.from_pretrained(model_name)
14
-
15
- print("Loading model...")
16
- model = AutoModelForCausalLM.from_pretrained(
17
- model_name,
18
- torch_dtype=torch.float16,
19
- device_map="auto",
20
- low_cpu_mem_usage=True
21
- )
22
 
23
  def respond(
24
  message,
@@ -28,40 +15,30 @@ def respond(
28
  temperature,
29
  top_p,
30
  ):
31
- # Format the conversation in Mistral's instruction format
32
- prompt = f"<s>[INST] {system_message} [/INST]\n\n"
33
-
34
- for user_msg, assistant_msg in history:
35
- if user_msg:
36
- prompt += f"[INST] {user_msg} [/INST]\n"
37
- if assistant_msg:
38
- prompt += f"{assistant_msg}\n\n"
39
-
40
- prompt += f"[INST] {message} [/INST]\n"
41
 
42
- # Print the prompt for debugging
43
- print(f"Prompt: {prompt}")
44
 
45
- # Encode the prompt
46
- inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
47
-
48
- # Generate tokens
49
- print("Generating response...")
50
-
51
- # Generate without streaming for simplicity
52
- generated_ids = model.generate(
53
- inputs.input_ids,
54
- max_new_tokens=max_tokens,
55
- do_sample=True,
56
  temperature=temperature,
57
  top_p=top_p,
58
- pad_token_id=tokenizer.eos_token_id,
59
- )
60
-
61
- # Decode the response
62
- full_response = tokenizer.decode(generated_ids[0][inputs.input_ids.shape[1]:], skip_special_tokens=True)
63
- print(f"Response generated: {full_response[:50]}...")
64
- return full_response
65
 
66
  """
67
  For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
@@ -69,20 +46,19 @@ For information on how to customize the ChatInterface, peruse the gradio docs: h
69
  demo = gr.ChatInterface(
70
  respond,
71
  additional_inputs=[
72
- gr.Textbox(value="You are a Microsoft 365 data management assistant specialized in SharePoint and OneDrive. Answer questions concisely and accurately.", label="System message"),
73
- gr.Slider(minimum=1, maximum=1024, value=256, step=1, label="Max new tokens"),
74
- gr.Slider(minimum=0.1, maximum=1.0, value=0.7, step=0.1, label="Temperature"),
75
  gr.Slider(
76
  minimum=0.1,
77
  maximum=1.0,
78
- value=0.9,
79
  step=0.05,
80
  label="Top-p (nucleus sampling)",
81
  ),
82
  ],
83
- title="Microsoft 365 Data Management Assistant",
84
- description="Ask questions about SharePoint, OneDrive, and other Microsoft 365 data management topics."
85
  )
86
 
 
87
  if __name__ == "__main__":
88
  demo.launch()
 
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,
 
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
  """
44
  For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
 
46
  demo = gr.ChatInterface(
47
  respond,
48
  additional_inputs=[
49
+ gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
+ gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
+ gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
  gr.Slider(
53
  minimum=0.1,
54
  maximum=1.0,
55
+ value=0.95,
56
  step=0.05,
57
  label="Top-p (nucleus sampling)",
58
  ),
59
  ],
 
 
60
  )
61
 
62
+
63
  if __name__ == "__main__":
64
  demo.launch()