Arjun Singh commited on
Commit
0c7e671
·
1 Parent(s): 300b9d3

adding a usage counter

Browse files
Files changed (1) hide show
  1. app.py +9 -10
app.py CHANGED
@@ -5,10 +5,8 @@ from huggingface_hub.utils import HfHubHTTPError
5
  system_prompt = """
6
  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.
7
  """
8
- usage_counter = 0
9
- def chat_with_Mixtral(prompt, hugging_face_api_key):
10
- global usage_counter
11
- usage_counter += 1
12
  model_name = "NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO"
13
  try:
14
  client = InferenceClient(model=model_name,token=hugging_face_api_key)
@@ -17,14 +15,14 @@ def chat_with_Mixtral(prompt, hugging_face_api_key):
17
  for completion in client.chat_completion(messages, max_tokens=500, stream=True):
18
  token = completion.choices[0].delta.content
19
  tokens += token
20
- yield tokens
21
  except HfHubHTTPError as e:
22
  error_message = str(e).strip()
23
  if error_message:
24
  if "Authorization header is correct, but the token seems invalid" in error_message:
25
- yield "Error: Invalid API token. Please check your Hugging Face API key."
26
  else:
27
- yield f"Error: An API error occurred - {error_message}"
28
 
29
  def clear_fields():
30
  return "", "" # Clear prompt and output, but not the API key
@@ -35,10 +33,10 @@ def clear_fields():
35
  with gr.Blocks(theme='freddyaboulton/test-blue') as demo:
36
  gr.Markdown("<center><h2>Arjun's Chatbot</h2></center>")
37
  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!")
38
- usage_display = gr.Markdown(f"This app has been used {usage_counter} times.")
39
  prompt = gr.Textbox(label='Question', lines=2, max_lines=5, placeholder = 'Type your question here.')
40
  token = gr.Textbox(label="Hugging Face API Key", type="password")
41
-
42
  with gr.Group():
43
  with gr.Row():
44
  submit_btn = gr.Button(value="Submit", elem_id="generate_button", variant="primary", size="sm")
@@ -47,7 +45,8 @@ with gr.Blocks(theme='freddyaboulton/test-blue') as demo:
47
  with gr.Row() as output:
48
  llm_output = gr.Markdown("<center><h3>AI Response</h3></center>")
49
 
50
- submit_btn.click(fn=chat_with_Mixtral, inputs = [prompt,token], outputs=llm_output)
 
51
  clear_btn.click(fn=clear_fields,outputs=[prompt,llm_output])
52
 
53
  demo.launch()
 
5
  system_prompt = """
6
  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.
7
  """
8
+
9
+ def chat_with_Mixtral(prompt, hugging_face_api_key,state):
 
 
10
  model_name = "NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO"
11
  try:
12
  client = InferenceClient(model=model_name,token=hugging_face_api_key)
 
15
  for completion in client.chat_completion(messages, max_tokens=500, stream=True):
16
  token = completion.choices[0].delta.content
17
  tokens += token
18
+ yield tokens,state+1
19
  except HfHubHTTPError as e:
20
  error_message = str(e).strip()
21
  if error_message:
22
  if "Authorization header is correct, but the token seems invalid" in error_message:
23
+ yield "Error: Invalid API token. Please check your Hugging Face API key.",state
24
  else:
25
+ yield f"Error: An API error occurred - {error_message}", state
26
 
27
  def clear_fields():
28
  return "", "" # Clear prompt and output, but not the API key
 
33
  with gr.Blocks(theme='freddyaboulton/test-blue') as demo:
34
  gr.Markdown("<center><h2>Arjun's Chatbot</h2></center>")
35
  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!")
36
+ usage_display = gr.Markdown("This app has been used 0 times.")
37
  prompt = gr.Textbox(label='Question', lines=2, max_lines=5, placeholder = 'Type your question here.')
38
  token = gr.Textbox(label="Hugging Face API Key", type="password")
39
+ state = gr.State(0)
40
  with gr.Group():
41
  with gr.Row():
42
  submit_btn = gr.Button(value="Submit", elem_id="generate_button", variant="primary", size="sm")
 
45
  with gr.Row() as output:
46
  llm_output = gr.Markdown("<center><h3>AI Response</h3></center>")
47
 
48
+
49
+ submit_btn.click(fn=chat_with_Mixtral, inputs = [prompt,token,state], outputs=[llm_output,state]).then(lambda s: f"This space has been used {s} times.", inputs=state,outputs=usage_display)
50
  clear_btn.click(fn=clear_fields,outputs=[prompt,llm_output])
51
 
52
  demo.launch()