wop commited on
Commit
aa36ade
·
1 Parent(s): c685066

Update app.py

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