goldenboy3332 commited on
Commit
ad2530a
·
verified ·
1 Parent(s): bfb2858

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -24
app.py CHANGED
@@ -1,11 +1,10 @@
 
 
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(
11
  message,
@@ -15,29 +14,37 @@ 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
  """
@@ -59,6 +66,5 @@ demo = gr.ChatInterface(
59
  ],
60
  )
61
 
62
-
63
  if __name__ == "__main__":
64
  demo.launch()
 
1
+ # Load model directly
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM
3
  import gradio as gr
 
 
 
 
 
 
4
 
5
+ # Load DialoGPT model and tokenizer
6
+ tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-large")
7
+ model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-large")
8
 
9
  def respond(
10
  message,
 
14
  temperature,
15
  top_p,
16
  ):
17
+ # Prepare the context from history
18
+ chat_history = ""
19
+ for user_input, bot_response in history:
20
+ if user_input:
21
+ chat_history += f"User: {user_input}\n"
22
+ if bot_response:
23
+ chat_history += f"Bot: {bot_response}\n"
24
 
25
+ # Append the new user message
26
+ chat_history += f"User: {message}\n"
27
 
28
+ # Tokenize the input
29
+ input_ids = tokenizer.encode(chat_history, return_tensors="pt")
30
 
31
+ # Generate response
32
+ output_ids = model.generate(
33
+ input_ids,
34
+ max_length=max_tokens + len(input_ids[0]),
35
  temperature=temperature,
36
  top_p=top_p,
37
+ pad_token_id=tokenizer.eos_token_id,
38
+ )
39
 
40
+ # Decode the output and get the response
41
+ output = tokenizer.decode(output_ids[0], skip_special_tokens=True)
42
+
43
+ # Extract the bot's response
44
+ bot_response = output.split("User:")[-1].split("Bot:")[-1].strip()
45
+ history.append((message, bot_response)) # Update history
46
+
47
+ yield bot_response
48
 
49
 
50
  """
 
66
  ],
67
  )
68
 
 
69
  if __name__ == "__main__":
70
  demo.launch()