Martín Bravo commited on
Commit
304c4d9
·
1 Parent(s): e541117

add: model

Browse files
Files changed (3) hide show
  1. app.py +33 -34
  2. requirements.txt +3 -1
  3. test.py +16 -0
app.py CHANGED
@@ -1,10 +1,20 @@
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("martinbravo/llama_finetuned_test")
8
 
9
 
10
  def respond(
@@ -15,34 +25,27 @@ 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
- """
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=[
@@ -51,14 +54,10 @@ demo = gr.ChatInterface(
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()
 
1
  import gradio as gr
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
3
+
4
+ # Load model and tokenizer
5
+ model_name = "martinbravo/llama_finetuned_test"
6
+ base_model_name = "unsloth/llama-3.2-1b-instruct-bnb-4bit"
7
+
8
+ # Load tokenizer and model locally
9
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
10
+ model = AutoModelForCausalLM.from_pretrained(
11
+ model_name,
12
+ device_map="auto", # Automatically maps model to GPU/CPU
13
+ trust_remote_code=True, # If model uses custom implementations
14
+ )
15
 
16
+ # Create a text-generation pipeline
17
+ generator = pipeline("text-generation", model=model, tokenizer=tokenizer, device=0)
 
 
18
 
19
 
20
  def respond(
 
25
  temperature,
26
  top_p,
27
  ):
28
+ # Build input prompt
29
+ prompt = system_message + "\n"
30
+ for user_input, assistant_response in history:
31
+ prompt += f"User: {user_input}\nAssistant: {assistant_response}\n"
32
+ prompt += f"User: {message}\nAssistant:"
33
+
34
+ # Generate response
35
+ response = generator(
36
+ prompt,
37
+ max_new_tokens=max_tokens,
 
 
 
 
 
 
38
  temperature=temperature,
39
  top_p=top_p,
40
+ do_sample=True, # Sampling for diverse responses
41
+ )[0]["generated_text"]
42
 
43
+ # Extract the assistant's response
44
+ assistant_response = response[len(prompt) :].strip()
45
+ yield assistant_response
46
 
47
 
48
+ # Gradio interface
 
 
49
  demo = gr.ChatInterface(
50
  respond,
51
  additional_inputs=[
 
54
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
55
  gr.Slider(
56
  minimum=0.1,
57
+ maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"
 
 
 
58
  ),
59
  ],
60
  )
61
 
 
62
  if __name__ == "__main__":
63
+ demo.launch()
requirements.txt CHANGED
@@ -1 +1,3 @@
1
- huggingface_hub==0.25.2
 
 
 
1
+ huggingface_hub==0.25.2
2
+ gradio
3
+ transformers
test.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoModel, AutoTokenizer
2
+
3
+ # Load the tokenizer
4
+ tokenizer = AutoTokenizer.from_pretrained("martinbravo/llama_finetuned_test")
5
+
6
+ # Load the model
7
+ model = AutoModel.from_pretrained("martinbravo/llama_finetuned_test")
8
+
9
+ # Test the model
10
+ input_text = "What is the capital of France?"
11
+ inputs = tokenizer(input_text, return_tensors="pt")
12
+
13
+ # Perform inference
14
+ outputs = model(**inputs)
15
+
16
+ print(outputs)