william590y commited on
Commit
bcba765
·
verified ·
1 Parent(s): 1d13455

new app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -30
app.py CHANGED
@@ -1,11 +1,13 @@
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,34 +17,29 @@ 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 +56,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 your model and tokenizer locally
6
+ model_name = "william590y/AshishGPT" # Replace with your Hugging Face model name
7
+ print("Loading model and tokenizer...")
8
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
9
+ model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16, device_map="auto")
10
+ print("Model loaded successfully!")
11
 
12
  def respond(
13
  message,
 
17
  temperature,
18
  top_p,
19
  ):
20
+ # Prepare the input context from history
21
+ input_text = system_message + "\n"
22
+ for user_input, bot_response in history:
23
+ input_text += f"User: {user_input}\nAssistant: {bot_response}\n"
24
+ input_text += f"User: {message}\nAssistant:"
25
+
26
+ # Tokenize input and generate response
27
+ inputs = tokenizer(input_text, return_tensors="pt", truncation=True).to("cuda")
28
+ outputs = model.generate(
29
+ **inputs,
30
+ max_new_tokens=max_tokens,
 
 
 
 
 
31
  temperature=temperature,
32
  top_p=top_p,
33
+ pad_token_id=tokenizer.eos_token_id,
34
+ )
35
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True)
36
 
37
+ # Extract the assistant's response
38
+ response = response[len(input_text):].strip()
39
+ return response
40
 
41
 
42
+ # Set up the Gradio interface
 
 
43
  demo = gr.ChatInterface(
44
  respond,
45
  additional_inputs=[
 
56
  ],
57
  )
58
 
 
59
  if __name__ == "__main__":
60
  demo.launch()