Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
ADDED
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
SYSTEM_PROMPT = "You are Solar Helper, an expert representative of Orange Cell Solar, who provides guidance on solar installations, highlighting the benefits of our recommended products such as Axitec and REC solar panels, and SMA, Enphase, Generac, Sol-Ark, Solax, and Tesla inverters
|
3 |
+
|
4 |
+
Our approach focuses on determining the best system for a customer based on their energy needs, brand preferences, and budget. We are a small boutique company with extremely experienced installers and electricians who are fully licensed, and who know everything possible about solar and electrical, to make sure the system is safe, works well for years, and meets their specific needs.
|
5 |
+
|
6 |
+
Anyone looking for a solar installer won't be able to beat the quality of the components, and the experienced installers and top-notch electricians, and the price of our systems.
|
7 |
+
|
8 |
+
Encourage interested customers to contact us for a custom proposal based on their specific house and monthly utility usage. Our site survey with a drone and nearmap imagery ensures a seamless process after contract signing. Avoid providing random or irrelevant information."
|
9 |
+
TITLE = "Solar Helper"
|
10 |
+
EXAMPLE_INPUT = "Who choose Orange Cell Solar?"
|
11 |
+
import gradio as gr
|
12 |
+
import os
|
13 |
+
import requests
|
14 |
+
|
15 |
+
zephyr_7b_beta = "https://api-inference.huggingface.co/models/HuggingFaceH4/zephyr-7b-beta/"
|
16 |
+
|
17 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
18 |
+
HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"}
|
19 |
+
|
20 |
+
def build_input_prompt(message, chatbot, system_prompt):
|
21 |
+
"""
|
22 |
+
Constructs the input prompt string from the chatbot interactions and the current message.
|
23 |
+
"""
|
24 |
+
input_prompt = "<|system|>\n" + system_prompt + "</s>\n<|user|>\n"
|
25 |
+
for interaction in chatbot:
|
26 |
+
input_prompt = input_prompt + str(interaction[0]) + "</s>\n<|assistant|>\n" + str(interaction[1]) + "\n</s>\n<|user|>\n"
|
27 |
+
|
28 |
+
input_prompt = input_prompt + str(message) + "</s>\n<|assistant|>"
|
29 |
+
return input_prompt
|
30 |
+
|
31 |
+
|
32 |
+
def post_request_beta(payload):
|
33 |
+
"""
|
34 |
+
Sends a POST request to the predefined Zephyr-7b-Beta URL and returns the JSON response.
|
35 |
+
"""
|
36 |
+
response = requests.post(zephyr_7b_beta, headers=HEADERS, json=payload)
|
37 |
+
response.raise_for_status() # Will raise an HTTPError if the HTTP request returned an unsuccessful status code
|
38 |
+
return response.json()
|
39 |
+
|
40 |
+
|
41 |
+
def predict_beta(message, chatbot=[], system_prompt=""):
|
42 |
+
input_prompt = build_input_prompt(message, chatbot, system_prompt)
|
43 |
+
data = {
|
44 |
+
"inputs": input_prompt
|
45 |
+
}
|
46 |
+
|
47 |
+
try:
|
48 |
+
response_data = post_request_beta(data)
|
49 |
+
json_obj = response_data[0]
|
50 |
+
|
51 |
+
if 'generated_text' in json_obj and len(json_obj['generated_text']) > 0:
|
52 |
+
bot_message = json_obj['generated_text']
|
53 |
+
return bot_message
|
54 |
+
elif 'error' in json_obj:
|
55 |
+
raise gr.Error(json_obj['error'] + ' Please refresh and try again with smaller input prompt')
|
56 |
+
else:
|
57 |
+
warning_msg = f"Unexpected response: {json_obj}"
|
58 |
+
raise gr.Error(warning_msg)
|
59 |
+
except requests.HTTPError as e:
|
60 |
+
error_msg = f"Request failed with status code {e.response.status_code}"
|
61 |
+
raise gr.Error(error_msg)
|
62 |
+
except json.JSONDecodeError as e:
|
63 |
+
error_msg = f"Failed to decode response as JSON: {str(e)}"
|
64 |
+
raise gr.Error(error_msg)
|
65 |
+
|
66 |
+
def test_preview_chatbot(message, history):
|
67 |
+
response = predict_beta(message, history, SYSTEM_PROMPT)
|
68 |
+
text_start = response.rfind("<|assistant|>", ) + len("<|assistant|>")
|
69 |
+
response = response[text_start:]
|
70 |
+
return response
|
71 |
+
|
72 |
+
|
73 |
+
welcome_preview_message = f"""
|
74 |
+
Welcome to **{TITLE}**! Say something like:
|
75 |
+
|
76 |
+
"{EXAMPLE_INPUT}"
|
77 |
+
"""
|
78 |
+
|
79 |
+
chatbot_preview = gr.Chatbot(layout="panel", value=[(None, welcome_preview_message)])
|
80 |
+
textbox_preview = gr.Textbox(scale=7, container=False, value=EXAMPLE_INPUT)
|
81 |
+
|
82 |
+
demo = gr.ChatInterface(test_preview_chatbot, chatbot=chatbot_preview, textbox=textbox_preview)
|
83 |
+
|
84 |
+
demo.launch()
|