ysharma HF staff commited on
Commit
129413a
1 Parent(s): cff6bc0

create app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -0
app.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import gradio as gr
3
+ import os
4
+
5
+
6
+ hf_token = os.getenv('HF_TOKEN')
7
+ api_url = os.getenv('API_URL')
8
+ headers = {
9
+ 'Content-Type': 'application/json',
10
+ }
11
+
12
+ 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."
13
+
14
+ def predict(message, chatbot):
15
+
16
+ print(f"message is - {message}")
17
+ print(f"chatbot is - {chatbot}")
18
+
19
+ input_prompt = f"[INST]<<SYS>>\n{system_message}\n<</SYS>>\n\n "
20
+ for interaction in chatbot:
21
+ input_prompt = input_prompt + interaction[0] + " [/INST] " + interaction[1] + " </s><s> [INST] "
22
+
23
+ input_prompt = input_prompt + message + " [/INST] "
24
+
25
+ print(f"input_prompt is - {input_prompt}")
26
+ data = {
27
+ "inputs": input_prompt,
28
+ "parameters": {"max_new_tokens":100}
29
+ }
30
+
31
+ response = requests.post(api_url, headers=headers, data=json.dumps(data), auth=('hf', hf_token))
32
+
33
+ print(f'API response is - {response.text}')
34
+ response_json_object = json.loads(response.text)
35
+ return response_json_object[0]['generated_text']
36
+
37
+
38
+ gr.ChatInterface(predict).queue().launch(debug= True)