wop commited on
Commit
f4d2332
·
1 Parent(s): 8bc4677

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -0
app.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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}"
70
+ """
71
+
72
+ chatbot_preview = gr.Chatbot(layout="panel", value=[(None, welcome_preview_message)])
73
+ 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()