File size: 3,262 Bytes
8d2cdf0 17ef741 4968d52 8d2cdf0 4968d52 0c7e671 e86400d 4968d52 6c8d0db 4968d52 e86400d 4968d52 20d3b15 e86400d 20d3b15 e86400d 4968d52 e86400d 4968d52 0c7e671 e86400d 4968d52 8d2cdf0 10349c3 17ef741 |
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 |
import gradio as gr
from huggingface_hub import InferenceClient
from huggingface_hub.utils import HfHubHTTPError
system_prompt = """
You 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. If 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. Your response should be complete, and not end abruptly.
"""
def chat_with_Mixtral(prompt, hugging_face_api_key):
model_name = "NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO"
try:
client = InferenceClient(model=model_name,token=hugging_face_api_key)
messages = [{"role": "system", "content": system_prompt},{"role": "user", "content": prompt}]
tokens = ""
for completion in client.chat_completion(messages, max_tokens=500, stream=True):
token = completion.choices[0].delta.content
tokens += token
yield tokens
except HfHubHTTPError as e:
error_message = str(e).strip()
if error_message:
if "Authorization header is correct, but the token seems invalid" in error_message:
yield "Error: Invalid API token. Please check your Hugging Face API key."
else:
yield f"Error: An API error occurred - {error_message}"
def clear_fields():
return "", "" # Clear prompt and output, but not the API key
#demo = gr.Interface(fn=chat_with_Mixtral, inputs=[gr.Textbox(label="Question"),gr.Textbox(label="Hugging Face API Key", type="password")],outputs="text")
with gr.Blocks(theme='freddyaboulton/test-blue') as demo:
gr.Markdown("<center><h2>Arjun's Chatbot</h2></center>")
gr.Markdown("Hi there! I'm an AI assistant tasked with answering one question at a time. I'm instructed to provide complete and truthful responses, without any harmful or biased content. I have some memory issues which are work in progress, so I cannot remember previous conversations, yet! In order to get a response, please provide your Hugging Face (HF) API Token along with your question. This token is unique for every HF user and can be found at [Access Tokens](https://huggingface.co/settings/tokens). Happy Chatting!")
prompt = gr.Textbox(label='Question', lines=2, max_lines=5, placeholder = 'Type your question here.')
token = gr.Textbox(label="Hugging Face API Key", type="password")
with gr.Group():
with gr.Row():
submit_btn = gr.Button(value="Submit", elem_id="generate_button", variant="primary", size="sm")
clear_btn = gr.ClearButton(value="Clear Question and AI Response", elem_id="clear_button", variant="secondary", size="sm")
with gr.Row() as output:
llm_output = gr.Markdown("<center><h3>AI Response</h3></center>")
submit_btn.click(fn=chat_with_Mixtral, inputs = [prompt,token], outputs=[llm_output])
clear_btn.click(fn=clear_fields,outputs=[prompt,llm_output])
demo.launch()
#test comment
|