Aksh1t commited on
Commit
ddab986
·
verified ·
1 Parent(s): f15b079

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -31
app.py CHANGED
@@ -1,11 +1,10 @@
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("Aksh1t/mistral-7b-oig-unsloth-merged")
8
 
 
 
 
 
9
 
10
  def respond(
11
  message,
@@ -15,33 +14,32 @@ def respond(
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=[
@@ -58,6 +56,5 @@ demo = gr.ChatInterface(
58
  ],
59
  )
60
 
61
-
62
  if __name__ == "__main__":
63
- demo.launch()
 
1
  import gradio as gr
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer
 
 
 
 
 
3
 
4
+ # Load the tokenizer and model
5
+ model_name = "Aksh1t/mistral-7b-oig-unsloth-merged"
6
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
7
+ model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto")
8
 
9
  def respond(
10
  message,
 
14
  temperature,
15
  top_p,
16
  ):
17
+ # Construct the prompt
18
+ prompt = system_message + "\n"
19
+ for user_msg, assistant_msg in history:
20
+ if user_msg:
21
+ prompt += f"User: {user_msg}\n"
22
+ if assistant_msg:
23
+ prompt += f"Assistant: {assistant_msg}\n"
24
+ prompt += f"User: {message}\nAssistant:"
25
+
26
+ # Encode the prompt and generate a response
27
+ inputs = tokenizer(prompt, return_tensors="pt")
28
+ outputs = model.generate(
29
+ inputs.input_ids,
30
+ max_new_tokens=max_tokens,
 
 
31
  temperature=temperature,
32
  top_p=top_p,
33
+ do_sample=True
34
+ )
35
+
36
+ # Decode the generated response
37
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True)
38
 
39
+ # Extract the assistant's reply
40
+ assistant_reply = response.split("Assistant:")[-1].strip()
41
+ yield assistant_reply
42
 
 
 
 
43
  demo = gr.ChatInterface(
44
  respond,
45
  additional_inputs=[
 
56
  ],
57
  )
58
 
 
59
  if __name__ == "__main__":
60
+ demo.launch()