Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
File size: 3,042 Bytes
129413a 42b930d 129413a a35571e b1dfaca 72f2f88 1beff8d 129413a 1beff8d 6f32c6f 129413a 6f32c6f 129413a 03449b0 129413a 09e4eaf 129413a 09e4eaf 129413a c1127b8 09e4eaf 129413a f95841a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
import json
import gradio as gr
import os
import requests
hf_token = os.getenv('HF_TOKEN')
api_url = os.getenv('API_URL')
headers = {
'Content-Type': 'application/json',
}
system_message = "\nYou are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.\n\nIf a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information."
title = "Llama2 70B Chatbot"
description = """This Space demonstrates model [Llama-2-70b-chat-hf](https://huggingface.co/meta-llama/Llama-2-70b-chat-hf) by Meta, running on Inference Endpoints using text-generation-inference. To have your own dedicated endpoint, you can [deploy it on Inference Endpoints](https://ui.endpoints.huggingface.co/). """
css = """.toast-wrap { display: none !important } """
def predict(message, chatbot):
print(f"Logging: message is - {message}")
print(f"Logging: chatbot is - {chatbot}")
input_prompt = f"[INST]<<SYS>>\n{system_message}\n<</SYS>>\n\n "
for interaction in chatbot:
input_prompt = input_prompt + interaction[0] + " [/INST] " + interaction[1] + " </s><s> [INST] "
input_prompt = input_prompt + message + " [/INST] "
print(f"Logging: input_prompt is - {input_prompt}")
data = {
"inputs": input_prompt,
"parameters": {"max_new_tokens":256}
}
#response = requests.post(api_url, headers=headers, data=json.dumps(data), auth=('hf', hf_token))
#print(f'Logging: API response is - {response.text}')
#response_json_object = json.loads(response.text)
#return response_json_object[0]['generated_text']
response = requests.post(api_url, headers=headers, data=json.dumps(data), auth=('hf', hf_token), stream=True)
partial_message = ""
for line in response.iter_lines():
if line: # filter out keep-alive new lines
# Decode from bytes to string
decoded_line = line.decode('utf-8')
# Remove 'data:' prefix
if decoded_line.startswith('data:'):
json_line = decoded_line[5:] # Exclude the first 5 characters ('data:')
else:
print("This line does not start with 'data:':", decoded_line)
continue
# Load as JSON
try:
#print(json.loads(json_line)['token']['text'])
partial_message = partial_message + json.loads(json_line)['token']['text']
yield partial_message
except json.JSONDecodeError:
gr.Warning("This line is not valid JSON: ", json_line)
continue
gr.ChatInterface(predict, title=title, description=description, css=css).queue(concurrency_count=75).launch()
|