Electricarchmage commited on
Commit
651361d
·
verified ·
1 Parent(s): fa7c232

hopefully fixed

Browse files
Files changed (1) hide show
  1. app.py +11 -6
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import gradio as gr
2
  from transformers import GPT2LMHeadModel, GPT2Tokenizer
 
3
 
4
  # Load model and tokenizer from Hugging Face Hub
5
  model_name = "Electricarchmage/cookbookgpt"
@@ -16,26 +17,30 @@ def respond(
16
  top_p,
17
  ):
18
  # Preparing the messages for context (the history and the new message)
19
- input_text = system_message + "\n"
20
 
 
21
  for val in history:
22
  if val[0]:
23
- input_text += f"User: {val[0]}\n"
24
  if val[1]:
25
- input_text += f"Assistant: {val[1]}\n"
26
-
27
- input_text += f"User: {message}\nAssistant:"
28
 
29
  # Tokenize the input and generate a response
30
- inputs = tokenizer(input_text, return_tensors="pt", max_length=1024, truncation=True)
 
31
 
32
  # Generate output tokens
33
  output = model.generate(
34
  inputs["input_ids"],
 
35
  max_length=max_tokens + len(inputs["input_ids"][0]),
36
  temperature=temperature,
37
  top_p=top_p,
38
  num_return_sequences=1,
 
39
  no_repeat_ngram_size=2,
40
  )
41
 
 
1
  import gradio as gr
2
  from transformers import GPT2LMHeadModel, GPT2Tokenizer
3
+ import torch
4
 
5
  # Load model and tokenizer from Hugging Face Hub
6
  model_name = "Electricarchmage/cookbookgpt"
 
17
  top_p,
18
  ):
19
  # Preparing the messages for context (the history and the new message)
20
+ messages = [{"role": "system", "content": system_message}]
21
 
22
+ # Convert history to the required format with 'role' and 'content'
23
  for val in history:
24
  if val[0]:
25
+ messages.append({"role": "user", "content": val[0]})
26
  if val[1]:
27
+ messages.append({"role": "assistant", "content": val[1]})
28
+
29
+ messages.append({"role": "user", "content": message})
30
 
31
  # Tokenize the input and generate a response
32
+ inputs = tokenizer([msg["content"] for msg in messages], return_tensors="pt", padding=True, truncation=True)
33
+ attention_mask = inputs.get('attention_mask', torch.ones_like(inputs['input_ids'])) # Default to ones if not provided
34
 
35
  # Generate output tokens
36
  output = model.generate(
37
  inputs["input_ids"],
38
+ attention_mask=attention_mask,
39
  max_length=max_tokens + len(inputs["input_ids"][0]),
40
  temperature=temperature,
41
  top_p=top_p,
42
  num_return_sequences=1,
43
+ do_sample=True, # Enable sampling for more dynamic responses
44
  no_repeat_ngram_size=2,
45
  )
46