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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -31
app.py CHANGED
@@ -1,55 +1,70 @@
1
- import os
2
- os.system("pip install torch transformers accelerate")
3
- from transformers import AutoModelForCausalLM, AutoTokenizer
4
- import gradio as gr
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
- )
19
 
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:
26
- input_prompt = input_prompt + str(interaction[0]) + "</s>\n\n" + str(interaction[1]) + "\n</s>\n\n"
27
 
28
- input_prompt = input_prompt + str(message) + "</s>\n"
29
  return input_prompt
30
 
 
 
 
 
 
 
 
 
 
 
31
  def predict_beta(message, chatbot=[], system_prompt=""):
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)
49
- text_start = response.rfind("") + len("")
50
  response = response[text_start:]
51
  return response
52
 
 
53
  welcome_preview_message = f"""
54
  Welcome to **{TITLE}**! Say something like:
55
  "{EXAMPLE_INPUT}"
@@ -60,4 +75,5 @@ textbox_preview = gr.Textbox(scale=7, container=False, value=EXAMPLE_INPUT)
60
 
61
  demo = gr.ChatInterface(test_preview_chatbot, chatbot=chatbot_preview, textbox=textbox_preview)
62
 
63
- demo.launch()
 
 
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
 
76
  demo = gr.ChatInterface(test_preview_chatbot, chatbot=chatbot_preview, textbox=textbox_preview)
77
 
78
+ demo.launch()
79
+