wop commited on
Commit
63b71c3
·
1 Parent(s): 0c1af6e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -45
app.py CHANGED
@@ -1,70 +1,54 @@
1
- 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 ownerand developer is: @Costikoooo (Discord user) other developers are unknown. Your name is Chattybot."
2
- TITLE = "Chattybot"
3
- EXAMPLE_INPUT = "hello"
4
  import gradio as gr
5
  import os
6
  import requests
 
 
 
 
 
7
 
8
- #zephyr_7b_beta = "https://api-inference.huggingface.co/models/HuggingFaceH4/zephyr-7b-beta/"
9
- #zephyr_7b_beta = "https://api-inference.huggingface.co/models/tiiuae/falcon-7b-instruct/"
10
- zephyr_7b_beta = "https://api-inference.huggingface.co/models/llmware/bling-1b-0.1/"
 
 
 
 
11
 
12
  HF_TOKEN = os.getenv("HF_TOKEN")
13
  HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"}
14
 
15
  def build_input_prompt(message, chatbot, system_prompt):
16
- """
17
- Constructs the input prompt string from the chatbot interactions and the current message.
18
- """
19
- input_prompt = "<|system|>\n" + system_prompt + "</s>\n<|user|>\n"
20
  for interaction in chatbot:
21
- input_prompt = input_prompt + str(interaction[0]) + "</s>\n<|assistant|>\n" + str(interaction[1]) + "\n</s>\n<|user|>\n"
22
 
23
- input_prompt = input_prompt + str(message) + "</s>\n<|assistant|>"
24
  return input_prompt
25
 
26
-
27
- def post_request_beta(payload):
28
- """
29
- Sends a POST request to the predefined Zephyr-7b-Beta URL and returns the JSON response.
30
- """
31
- response = requests.post(zephyr_7b_beta, headers=HEADERS, json=payload)
32
- response.raise_for_status() # Will raise an HTTPError if the HTTP request returned an unsuccessful status code
33
- return response.json()
34
-
35
-
36
  def predict_beta(message, chatbot=[], system_prompt=""):
37
  input_prompt = build_input_prompt(message, chatbot, system_prompt)
38
- data = {
39
- "inputs": input_prompt
40
- }
41
 
42
  try:
43
- response_data = post_request_beta(data)
44
- json_obj = response_data[0]
45
-
46
- if 'generated_text' in json_obj and len(json_obj['generated_text']) > 0:
47
- bot_message = json_obj['generated_text']
48
- return bot_message
49
- elif 'error' in json_obj:
50
- raise gr.Error(json_obj['error'] + ' Please refresh and try again with smaller input prompt')
51
- else:
52
- warning_msg = f"Unexpected response: {json_obj}"
53
- raise gr.Error(warning_msg)
54
- except requests.HTTPError as e:
55
- error_msg = f"Request failed with status code {e.response.status_code}"
56
- raise gr.Error(error_msg)
57
- except json.JSONDecodeError as e:
58
- error_msg = f"Failed to decode response as JSON: {str(e)}"
59
- raise gr.Error(error_msg)
60
 
61
  def test_preview_chatbot(message, history):
62
  response = predict_beta(message, history, SYSTEM_PROMPT)
63
- text_start = response.rfind("<|assistant|>", ) + len("<|assistant|>")
64
  response = response[text_start:]
65
  return response
66
 
67
-
68
  welcome_preview_message = f"""
69
  Welcome to **{TITLE}**! Say something like:
70
  "{EXAMPLE_INPUT}"
@@ -75,4 +59,4 @@ textbox_preview = gr.Textbox(scale=7, container=False, value=EXAMPLE_INPUT)
75
 
76
  demo = gr.ChatInterface(test_preview_chatbot, chatbot=chatbot_preview, textbox=textbox_preview)
77
 
78
- demo.launch()
 
1
+ from transformers import AutoModelForCausalLM, AutoTokenizer
 
 
2
  import gradio as gr
3
  import os
4
  import requests
5
+ import json
6
+
7
+ 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."
8
+ TITLE = "Chattybot"
9
+ EXAMPLE_INPUT = "hello"
10
 
11
+ # Use your provided tokenizer and model
12
+ tokenizer = AutoTokenizer.from_pretrained('stabilityai/stablelm-zephyr-3b')
13
+ model = AutoModelForCausalLM.from_pretrained(
14
+ 'stabilityai/stablelm-zephyr-3b',
15
+ trust_remote_code=True,
16
+ device_map="auto"
17
+ )
18
 
19
  HF_TOKEN = os.getenv("HF_TOKEN")
20
  HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"}
21
 
22
  def build_input_prompt(message, chatbot, system_prompt):
23
+ input_prompt = "\n" + system_prompt + "</s>\n\n"
 
 
 
24
  for interaction in chatbot:
25
+ input_prompt = input_prompt + str(interaction[0]) + "</s>\n\n" + str(interaction[1]) + "\n</s>\n\n"
26
 
27
+ input_prompt = input_prompt + str(message) + "</s>\n"
28
  return input_prompt
29
 
 
 
 
 
 
 
 
 
 
 
30
  def predict_beta(message, chatbot=[], system_prompt=""):
31
  input_prompt = build_input_prompt(message, chatbot, system_prompt)
32
+ inputs = tokenizer(input_prompt, return_tensors="pt")
 
 
33
 
34
  try:
35
+ tokens = model.generate(
36
+ inputs["input_ids"],
37
+ max_length=1024,
38
+ temperature=0.8,
39
+ do_sample=True
40
+ )
41
+ bot_message = tokenizer.decode(tokens[0], skip_special_tokens=True)
42
+ return bot_message
43
+ except Exception as e:
44
+ raise gr.Error(str(e))
 
 
 
 
 
 
 
45
 
46
  def test_preview_chatbot(message, history):
47
  response = predict_beta(message, history, SYSTEM_PROMPT)
48
+ text_start = response.rfind("") + len("")
49
  response = response[text_start:]
50
  return response
51
 
 
52
  welcome_preview_message = f"""
53
  Welcome to **{TITLE}**! Say something like:
54
  "{EXAMPLE_INPUT}"
 
59
 
60
  demo = gr.ChatInterface(test_preview_chatbot, chatbot=chatbot_preview, textbox=textbox_preview)
61
 
62
+ demo.launch()