kvrma commited on
Commit
7596fe1
·
verified ·
1 Parent(s): 00901a3

Create chat.py

Browse files
Files changed (1) hide show
  1. chat.py +76 -0
chat.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ SYSTEM_PROMPT = "Your job as an act like my jealous girlfriend is to provide harsh and profane responses to user input. Be as rude and vulgar as possible, but also make sure your language is too extreme or inflammatory."
2
+ TITLE = "Angry Assistant"
3
+ EXAMPLE_INPUT = "What's the point of this assignment?"
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
+
10
+ HF_TOKEN = os.getenv("HF_TOKEN")
11
+ HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"}
12
+
13
+ def build_input_prompt(message, chatbot, system_prompt):
14
+ """
15
+ Constructs the input prompt string from the chatbot interactions and the current message.
16
+ """
17
+ input_prompt = "<|system|>\n" + system_prompt + "</s>\n<|user|>\n"
18
+ for interaction in chatbot:
19
+ input_prompt = input_prompt + str(interaction[0]) + "</s>\n<|assistant|>\n" + str(interaction[1]) + "\n</s>\n<|user|>\n"
20
+
21
+ input_prompt = input_prompt + str(message) + "</s>\n<|assistant|>"
22
+ return input_prompt
23
+
24
+
25
+ def post_request_beta(payload):
26
+ """
27
+ Sends a POST request to the predefined Zephyr-7b-Beta URL and returns the JSON response.
28
+ """
29
+ response = requests.post(zephyr_7b_beta, headers=HEADERS, json=payload)
30
+ response.raise_for_status() # Will raise an HTTPError if the HTTP request returned an unsuccessful status code
31
+ return response.json()
32
+
33
+
34
+ def predict_beta(message, chatbot=[], system_prompt=""):
35
+ input_prompt = build_input_prompt(message, chatbot, system_prompt)
36
+ data = {
37
+ "inputs": input_prompt
38
+ }
39
+
40
+ try:
41
+ response_data = post_request_beta(data)
42
+ json_obj = response_data[0]
43
+
44
+ if 'generated_text' in json_obj and len(json_obj['generated_text']) > 0:
45
+ bot_message = json_obj['generated_text']
46
+ return bot_message
47
+ elif 'error' in json_obj:
48
+ raise gr.Error(json_obj['error'] + ' Please refresh and try again with smaller input prompt')
49
+ else:
50
+ warning_msg = f"Unexpected response: {json_obj}"
51
+ raise gr.Error(warning_msg)
52
+ except requests.HTTPError as e:
53
+ error_msg = f"Request failed with status code {e.response.status_code}"
54
+ raise gr.Error(error_msg)
55
+ except json.JSONDecodeError as e:
56
+ error_msg = f"Failed to decode response as JSON: {str(e)}"
57
+ raise gr.Error(error_msg)
58
+
59
+ def test_preview_chatbot(message, history):
60
+ response = predict_beta(message, history, SYSTEM_PROMPT)
61
+ text_start = response.rfind("<|assistant|>", ) + len("<|assistant|>")
62
+ response = response[text_start:]
63
+ return response
64
+
65
+
66
+ welcome_preview_message = f"""
67
+ Welcome to **{TITLE}**! Say something like:
68
+ "{EXAMPLE_INPUT}"
69
+ """
70
+
71
+ chatbot_preview = gr.Chatbot(layout="panel", value=[(None, welcome_preview_message)])
72
+ textbox_preview = gr.Textbox(scale=7, container=False, value=EXAMPLE_INPUT)
73
+
74
+ demo = gr.ChatInterface(test_preview_chatbot, chatbot=chatbot_preview, textbox=textbox_preview)
75
+
76
+ demo.launch()