wop commited on
Commit
9f6a9bd
·
1 Parent(s): b22ad80

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -15
app.py CHANGED
@@ -5,16 +5,14 @@ import gradio as gr
5
  import requests
6
  import json
7
 
8
- from accelerate import Accelerator
9
-
10
  SYSTEM_PROMPT = "As a generative chatbot (you are not a GPT but your structure is 50% the same), your primary function is to provide helpful and friendly responses to user queries. Feel free to add some personality, but make sure your responses are accurate and helpful. Your owner and developer is: @Costikoooo (Discord user) other developers are unknown. Your name is Chattybot."
11
  TITLE = "Chattybot"
12
  EXAMPLE_INPUT = "hello"
13
 
14
- # Use your provided tokenizer and model
15
- tokenizer = AutoTokenizer.from_pretrained('stabilityai/stablelm-zephyr-3b')
16
  model = AutoModelForCausalLM.from_pretrained(
17
- 'stabilityai/stablelm-zephyr-3b',
18
  trust_remote_code=True,
19
  device_map="auto"
20
  )
@@ -22,12 +20,6 @@ model = AutoModelForCausalLM.from_pretrained(
22
  HF_TOKEN = os.getenv("HF_TOKEN")
23
  HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"}
24
 
25
- # Initialize Accelerator
26
- accelerator = Accelerator()
27
-
28
- # Wrap model and tokenizer with accelerator
29
- model, tokenizer = accelerator.prepare(model, tokenizer)
30
-
31
  def build_input_prompt(message, chatbot, system_prompt):
32
  input_prompt = "\n" + system_prompt + "</s>\n\n"
33
  for interaction in chatbot:
@@ -40,15 +32,17 @@ def predict_beta(message, chatbot=[], system_prompt=""):
40
  input_prompt = build_input_prompt(message, chatbot, system_prompt)
41
  inputs = tokenizer(input_prompt, return_tensors="pt")
42
 
43
- with accelerator.device():
44
  tokens = model.generate(
45
  inputs["input_ids"],
46
  max_length=1024,
47
  temperature=0.8,
48
  do_sample=True
49
  )
50
- bot_message = tokenizer.decode(tokens[0], skip_special_tokens=True)
51
- return bot_message
 
 
52
 
53
  def test_preview_chatbot(message, history):
54
  response = predict_beta(message, history, SYSTEM_PROMPT)
@@ -66,4 +60,4 @@ textbox_preview = gr.Textbox(scale=7, container=False, value=EXAMPLE_INPUT)
66
 
67
  demo = gr.ChatInterface(test_preview_chatbot, chatbot=chatbot_preview, textbox=textbox_preview)
68
 
69
- demo.launch()
 
5
  import requests
6
  import json
7
 
 
 
8
  SYSTEM_PROMPT = "As a generative chatbot (you are not a GPT but your structure is 50% the same), your primary function is to provide helpful and friendly responses to user queries. Feel free to add some personality, but make sure your responses are accurate and helpful. Your owner and developer is: @Costikoooo (Discord user) other developers are unknown. Your name is Chattybot."
9
  TITLE = "Chattybot"
10
  EXAMPLE_INPUT = "hello"
11
 
12
+ # Use a smaller model (EleutherAI/gpt-neo-125M)
13
+ tokenizer = AutoTokenizer.from_pretrained('EleutherAI/gpt-neo-125M')
14
  model = AutoModelForCausalLM.from_pretrained(
15
+ 'EleutherAI/gpt-neo-125M',
16
  trust_remote_code=True,
17
  device_map="auto"
18
  )
 
20
  HF_TOKEN = os.getenv("HF_TOKEN")
21
  HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"}
22
 
 
 
 
 
 
 
23
  def build_input_prompt(message, chatbot, system_prompt):
24
  input_prompt = "\n" + system_prompt + "</s>\n\n"
25
  for interaction in chatbot:
 
32
  input_prompt = build_input_prompt(message, chatbot, system_prompt)
33
  inputs = tokenizer(input_prompt, return_tensors="pt")
34
 
35
+ try:
36
  tokens = model.generate(
37
  inputs["input_ids"],
38
  max_length=1024,
39
  temperature=0.8,
40
  do_sample=True
41
  )
42
+ bot_message = tokenizer.decode(tokens[0], skip_special_tokens=True)
43
+ return bot_message
44
+ except Exception as e:
45
+ raise gr.Error(str(e))
46
 
47
  def test_preview_chatbot(message, history):
48
  response = predict_beta(message, history, SYSTEM_PROMPT)
 
60
 
61
  demo = gr.ChatInterface(test_preview_chatbot, chatbot=chatbot_preview, textbox=textbox_preview)
62
 
63
+ demo.launch()