Update app_template.py
Browse files- app_template.py +5 -34
app_template.py
CHANGED
|
@@ -5,39 +5,12 @@ import requests
|
|
| 5 |
|
| 6 |
tulu = "https://tonic1-tulu.hf.space/--replicas/t5vxm/"
|
| 7 |
|
| 8 |
-
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 9 |
-
HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 10 |
-
|
| 11 |
-
def build_input_prompt(message, chatbot, system_prompt):
|
| 12 |
-
"""
|
| 13 |
-
Constructs the input prompt string from the chatbot interactions and the current message.
|
| 14 |
-
"""
|
| 15 |
-
input_prompt = "<|system|>\n" + system_prompt + "</s>\n<|user|>\n"
|
| 16 |
-
for interaction in chatbot:
|
| 17 |
-
input_prompt = input_prompt + str(interaction[0]) + "</s>\n<|assistant|>\n" + str(interaction[1]) + "\n</s>\n<|user|>\n"
|
| 18 |
-
|
| 19 |
-
input_prompt = input_prompt + str(message) + "</s>\n<|assistant|>"
|
| 20 |
-
return input_prompt
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
def post_request_beta(payload):
|
| 24 |
-
"""
|
| 25 |
-
Sends a POST request to the predefined Tulu URL and returns the JSON response.
|
| 26 |
-
"""
|
| 27 |
-
response = requests.post(tulu, headers=HEADERS, json=payload)
|
| 28 |
-
response.raise_for_status() # Will raise an HTTPError if the HTTP request returned an unsuccessful status code
|
| 29 |
-
return response.json()
|
| 30 |
-
|
| 31 |
|
| 32 |
def predict_beta(message, chatbot=[], system_prompt=""):
|
| 33 |
-
client = Client(tulu)
|
| 34 |
-
|
| 35 |
-
# Build the input prompt
|
| 36 |
-
input_prompt = build_input_prompt(message, chatbot, system_prompt) # Ensure this function is defined
|
| 37 |
|
| 38 |
try:
|
| 39 |
-
|
| 40 |
-
max_new_tokens = 1200
|
| 41 |
temperature = 0.4
|
| 42 |
top_p = 0.9
|
| 43 |
repetition_penalty = 0.9
|
|
@@ -45,7 +18,8 @@ def predict_beta(message, chatbot=[], system_prompt=""):
|
|
| 45 |
|
| 46 |
# Making the prediction
|
| 47 |
result = client.predict(
|
| 48 |
-
|
|
|
|
| 49 |
max_new_tokens,
|
| 50 |
temperature,
|
| 51 |
top_p,
|
|
@@ -54,9 +28,8 @@ def predict_beta(message, chatbot=[], system_prompt=""):
|
|
| 54 |
fn_index=0
|
| 55 |
)
|
| 56 |
|
| 57 |
-
# Extracting the response
|
| 58 |
if result is not None and len(result) > 0:
|
| 59 |
-
bot_message = result[0]
|
| 60 |
return bot_message
|
| 61 |
else:
|
| 62 |
raise gr.Error("No response received from the model.")
|
|
@@ -67,8 +40,6 @@ def predict_beta(message, chatbot=[], system_prompt=""):
|
|
| 67 |
|
| 68 |
def test_preview_chatbot(message, history):
|
| 69 |
response = predict_beta(message, history, SYSTEM_PROMPT)
|
| 70 |
-
text_start = response.rfind("<|assistant|>", ) + len("<|assistant|>")
|
| 71 |
-
response = response[text_start:]
|
| 72 |
return response
|
| 73 |
|
| 74 |
|
|
|
|
| 5 |
|
| 6 |
tulu = "https://tonic1-tulu.hf.space/--replicas/t5vxm/"
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
def predict_beta(message, chatbot=[], system_prompt=""):
|
| 10 |
+
client = Client(tulu)
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
try:
|
| 13 |
+
max_new_tokens = 800
|
|
|
|
| 14 |
temperature = 0.4
|
| 15 |
top_p = 0.9
|
| 16 |
repetition_penalty = 0.9
|
|
|
|
| 18 |
|
| 19 |
# Making the prediction
|
| 20 |
result = client.predict(
|
| 21 |
+
message,
|
| 22 |
+
system_prompt
|
| 23 |
max_new_tokens,
|
| 24 |
temperature,
|
| 25 |
top_p,
|
|
|
|
| 28 |
fn_index=0
|
| 29 |
)
|
| 30 |
|
|
|
|
| 31 |
if result is not None and len(result) > 0:
|
| 32 |
+
bot_message = result[0]
|
| 33 |
return bot_message
|
| 34 |
else:
|
| 35 |
raise gr.Error("No response received from the model.")
|
|
|
|
| 40 |
|
| 41 |
def test_preview_chatbot(message, history):
|
| 42 |
response = predict_beta(message, history, SYSTEM_PROMPT)
|
|
|
|
|
|
|
| 43 |
return response
|
| 44 |
|
| 45 |
|