Grandediw commited on
Commit
1db5cfd
·
1 Parent(s): 50cc010
Files changed (1) hide show
  1. app.py +50 -38
app.py CHANGED
@@ -1,52 +1,64 @@
1
- import os
2
  import gradio as gr
3
- import torch
4
- from transformers import AutoTokenizer, AutoModel
5
- from safetensors.torch import load_file
6
 
7
- # Load the Hugging Face API token from environment variable
8
- token = os.getenv("HUGGINGFACE_API_TOKEN")
9
- if not token:
10
- raise ValueError("HUGGINGFACE_API_TOKEN is not set. Please add it in the Secrets section of your Space.")
11
 
12
- # Configure device
13
- device = "cuda" if torch.cuda.is_available() else "cpu"
14
 
15
- # Load the tokenizer and model using the token
16
- model_repo = "Grandediw/lora_model"
17
- tokenizer = AutoTokenizer.from_pretrained(model_repo, token=token)
18
- base_model = AutoModel.from_pretrained(model_repo, token=token)
 
 
 
 
 
19
 
20
- # Load the LoRA adapter weights
21
- lora_weights_path = "adapter_model.safetensors"
22
- lora_weights = load_file(lora_weights_path)
 
 
23
 
24
- # Apply LoRA weights to the base model
25
- for name, param in base_model.named_parameters():
26
- if name in lora_weights:
27
- param.data += lora_weights[name].to(device, dtype=param.dtype)
28
 
29
- # Move the model to the device
30
- base_model = base_model.to(device)
31
 
32
- # Define the inference function
33
- def infer(prompt):
34
- inputs = tokenizer(prompt, return_tensors="pt").to(device)
35
- outputs = base_model(**inputs)
36
- # Placeholder return, modify based on your specific model task
37
- return outputs.last_hidden_state.mean(dim=1).cpu().detach().numpy()
 
 
38
 
39
- # Gradio interface Update
40
- with gr.Blocks() as demo:
41
- gr.Markdown("## LoRA Model Inference")
42
 
43
- with gr.Row():
44
- prompt = gr.Textbox(label="Prompt", placeholder="Enter your prompt here...")
45
- generate_button = gr.Button("Generate")
46
 
47
- output = gr.Textbox(label="Output")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
 
49
- generate_button.click(fn=infer, inputs=[prompt], outputs=[output])
50
 
51
  if __name__ == "__main__":
52
- 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("Grandediw/lora_model")
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
+ """
44
+ For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
+ """
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()