desert commited on
Commit
2af305a
·
1 Parent(s): 408d189

init inference

Browse files
Files changed (1) hide show
  1. app.py +31 -39
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("Mat17892/llama_lora_G14")
8
 
 
 
 
 
9
 
10
  def respond(
11
  message,
@@ -15,47 +14,40 @@ def respond(
15
  temperature,
16
  top_p,
17
  ):
18
- # Combine the system message and chat history into a single string
19
- prompt = system_message + "\n"
20
- for user_input, assistant_reply in history:
21
- if user_input:
22
- prompt += f"User: {user_input}\n"
23
- if assistant_reply:
24
- prompt += f"Assistant: {assistant_reply}\n"
25
- prompt += f"User: {message}\nAssistant:"
26
 
27
- # Send the request to the model
28
- response = ""
29
- for token in client.text_generation(
30
- prompt,
31
- max_new_tokens=max_tokens,
32
- stream=True,
33
- temperature=temperature,
34
- top_p=top_p,
35
- ):
36
- response += token.token
37
- yield response
38
 
39
- """
40
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
41
- """
 
 
 
 
 
 
 
 
 
 
 
 
42
  demo = gr.ChatInterface(
43
  respond,
44
- type="messages",
45
  additional_inputs=[
46
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
47
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
48
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
49
- gr.Slider(
50
- minimum=0.1,
51
- maximum=1.0,
52
- value=0.95,
53
- step=0.05,
54
- label="Top-p (nucleus sampling)",
55
- ),
56
  ],
57
  )
58
 
59
-
60
  if __name__ == "__main__":
61
- demo.launch()
 
1
  import gradio as gr
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer
 
 
 
 
 
3
 
4
+ # Load your model and tokenizer
5
+ model_name = "Mat17892/llama_lora_model_1" # Replace with your Hugging Face model name
6
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
7
+ model = AutoModelForCausalLM.from_pretrained(model_name)
8
 
9
  def respond(
10
  message,
 
14
  temperature,
15
  top_p,
16
  ):
17
+ messages = [{"role": "system", "content": system_message}]
18
+
19
+ for val in history:
20
+ if val[0]:
21
+ messages.append({"role": "user", "content": val[0]})
22
+ if val[1]:
23
+ messages.append({"role": "assistant", "content": val[1]})
 
24
 
25
+ messages.append({"role": "user", "content": message})
 
 
 
 
 
 
 
 
 
 
26
 
27
+ # Prepare input for the model
28
+ input_text = message
29
+ inputs = tokenizer(input_text, return_tensors="pt")
30
+
31
+ # Generate response
32
+ outputs = model.generate(
33
+ **inputs,
34
+ max_new_tokens=max_tokens,
35
+ temperature=temperature,
36
+ top_p=top_p
37
+ )
38
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True)
39
+ return response
40
+
41
+ # Create the Gradio interface
42
  demo = gr.ChatInterface(
43
  respond,
 
44
  additional_inputs=[
45
+ gr.Textbox(value="You are a friendly chatbot.", label="System message"),
46
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
47
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
48
+ gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p"),
 
 
 
 
 
 
49
  ],
50
  )
51
 
 
52
  if __name__ == "__main__":
53
+ demo.launch()