eronariodito commited on
Commit
8d3a5e0
·
verified ·
1 Parent(s): dde3b7f

Trying with existing transformer

Browse files
Files changed (1) hide show
  1. app.py +33 -19
app.py CHANGED
@@ -1,10 +1,15 @@
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(
@@ -15,8 +20,8 @@ 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]})
@@ -25,24 +30,34 @@ def respond(
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=[
@@ -59,6 +74,5 @@ demo = gr.ChatInterface(
59
  ],
60
  )
61
 
62
-
63
  if __name__ == "__main__":
64
  demo.launch()
 
1
  import gradio as gr
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer
3
+ import torch
4
 
5
+ # Load model and tokenizer directly
6
+ model_name = "jdowling/lora_model"
7
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
8
+ model = AutoModelForCausalLM.from_pretrained(model_name)
9
+
10
+ # Move the model to the appropriate device (GPU if available, else CPU)
11
+ device = "cuda" if torch.cuda.is_available() else "cpu"
12
+ model.to(device)
13
 
14
 
15
  def respond(
 
20
  temperature,
21
  top_p,
22
  ):
23
+ # Prepare prompt with history
24
  messages = [{"role": "system", "content": system_message}]
 
25
  for val in history:
26
  if val[0]:
27
  messages.append({"role": "user", "content": val[0]})
 
30
 
31
  messages.append({"role": "user", "content": message})
32
 
33
+ # Convert conversation into a single input string
34
+ prompt = f"{system_message}\n"
35
+ for turn in messages[1:]:
36
+ if turn["role"] == "user":
37
+ prompt += f"User: {turn['content']}\n"
38
+ elif turn["role"] == "assistant":
39
+ prompt += f"Assistant: {turn['content']}\n"
40
+ prompt += "Assistant:"
41
+
42
+ # Tokenize input
43
+ inputs = tokenizer(prompt, return_tensors="pt").to(device)
44
 
45
+ # Generate response
46
+ output = model.generate(
47
+ inputs["input_ids"],
48
+ max_length=inputs["input_ids"].shape[1] + max_tokens,
49
  temperature=temperature,
50
  top_p=top_p,
51
+ pad_token_id=tokenizer.eos_token_id,
52
+ )
53
 
54
+ # Decode response and extract the new assistant message
55
+ response = tokenizer.decode(output[0], skip_special_tokens=True)
56
+ response = response[len(prompt):].strip() # Strip the input part from the response
57
+
58
+ yield response
59
 
60
 
 
 
 
61
  demo = gr.ChatInterface(
62
  respond,
63
  additional_inputs=[
 
74
  ],
75
  )
76
 
 
77
  if __name__ == "__main__":
78
  demo.launch()