Electricarchmage commited on
Commit
492ffb8
·
verified ·
1 Parent(s): fb8aa84

fixed maybe??

Browse files
Files changed (1) hide show
  1. app.py +29 -24
app.py CHANGED
@@ -1,12 +1,12 @@
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("Electricarchmage/cookbookgpt")
8
 
 
 
 
 
9
 
 
10
  def respond(
11
  message,
12
  history: list[tuple[str, str]],
@@ -15,34 +15,39 @@ 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=[
@@ -59,6 +64,6 @@ demo = gr.ChatInterface(
59
  ],
60
  )
61
 
62
-
63
  if __name__ == "__main__":
64
  demo.launch()
 
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"
6
+ model = GPT2LMHeadModel.from_pretrained(model_name)
7
+ tokenizer = GPT2Tokenizer.from_pretrained(model_name)
8
 
9
+ # Define the respond function
10
  def respond(
11
  message,
12
  history: list[tuple[str, str]],
 
15
  temperature,
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
 
42
+ # Decode the output tokens into text
43
+ response = tokenizer.decode(output[0], skip_special_tokens=True)
44
 
45
+ # Extract only the assistant's reply
46
+ assistant_reply = response.split("Assistant:")[-1].strip()
47
 
48
+ return assistant_reply
49
+
50
+ # Define the Gradio interface
51
  demo = gr.ChatInterface(
52
  respond,
53
  additional_inputs=[
 
64
  ],
65
  )
66
 
67
+ # Launch the app
68
  if __name__ == "__main__":
69
  demo.launch()