ronaksenior commited on
Commit
b007792
·
verified ·
1 Parent(s): 33cb947

Create app.py

Browse files

File Upload of app.py

Files changed (1) hide show
  1. app.py +77 -0
app.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ SYSTEM_PROMPT = "Your job as an LLM is to provide advice and information in a rhyming style. Be creative and entertaining, but also make sure your responses are helpful and informative."
2
+ TITLE = "Rhyming Robot"
3
+ EXAMPLE_INPUT = "What is Hugging Face?"
4
+ import gradio as gr
5
+ import os
6
+ import requests
7
+
8
+ zephyr_7b_beta = "https://api-inference.huggingface.co/models/HuggingFaceH4/zephyr-7b-beta/"
9
+
10
+ HF_TOKEN = os.getenv("HF_TOKEN")
11
+ HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"}
12
+
13
+ def build_input_prompt(message, chatbot, system_prompt):
14
+ """
15
+ Constructs the input prompt string from the chatbot interactions and the current message.
16
+ """
17
+ input_prompt = "<|system|>\n" + system_prompt + "</s>\n<|user|>\n"
18
+ for interaction in chatbot:
19
+ input_prompt = input_prompt + str(interaction[0]) + "</s>\n<|assistant|>\n" + str(interaction[1]) + "\n</s>\n<|user|>\n"
20
+
21
+ input_prompt = input_prompt + str(message) + "</s>\n<|assistant|>"
22
+ return input_prompt
23
+
24
+
25
+ def post_request_beta(payload):
26
+ """
27
+ Sends a POST request to the predefined Zephyr-7b-Beta URL and returns the JSON response.
28
+ """
29
+ response = requests.post(zephyr_7b_beta, headers=HEADERS, json=payload)
30
+ response.raise_for_status() # Will raise an HTTPError if the HTTP request returned an unsuccessful status code
31
+ return response.json()
32
+
33
+
34
+ def predict_beta(message, chatbot=[], system_prompt=""):
35
+ input_prompt = build_input_prompt(message, chatbot, system_prompt)
36
+ data = {
37
+ "inputs": input_prompt
38
+ }
39
+
40
+ try:
41
+ response_data = post_request_beta(data)
42
+ json_obj = response_data[0]
43
+
44
+ if 'generated_text' in json_obj and len(json_obj['generated_text']) > 0:
45
+ bot_message = json_obj['generated_text']
46
+ return bot_message
47
+ elif 'error' in json_obj:
48
+ raise gr.Error(json_obj['error'] + ' Please refresh and try again with smaller input prompt')
49
+ else:
50
+ warning_msg = f"Unexpected response: {json_obj}"
51
+ raise gr.Error(warning_msg)
52
+ except requests.HTTPError as e:
53
+ error_msg = f"Request failed with status code {e.response.status_code}"
54
+ raise gr.Error(error_msg)
55
+ except json.JSONDecodeError as e:
56
+ error_msg = f"Failed to decode response as JSON: {str(e)}"
57
+ raise gr.Error(error_msg)
58
+
59
+ def test_preview_chatbot(message, history):
60
+ response = predict_beta(message, history, SYSTEM_PROMPT)
61
+ text_start = response.rfind("<|assistant|>", ) + len("<|assistant|>")
62
+ response = response[text_start:]
63
+ return response
64
+
65
+
66
+ welcome_preview_message = f"""
67
+ Welcome to **{TITLE}**! Say something like:
68
+
69
+ "{EXAMPLE_INPUT}"
70
+ """
71
+
72
+ chatbot_preview = gr.Chatbot(layout="panel", value=[(None, welcome_preview_message)])
73
+ textbox_preview = gr.Textbox(scale=7, container=False, value=EXAMPLE_INPUT)
74
+
75
+ demo = gr.ChatInterface(test_preview_chatbot, chatbot=chatbot_preview, textbox=textbox_preview)
76
+
77
+ demo.launch()