Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,33 +1,49 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import requests
|
| 3 |
import json
|
| 4 |
-
from huggingface_hub import HfApi
|
| 5 |
-
import huggingface_hub
|
| 6 |
-
import os
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
zephyr_7b_beta = "https://api-inference.huggingface.co/models/HuggingFaceH4/zephyr-7b-beta/"
|
| 10 |
-
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 11 |
HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
def build_input_prompt(message, chatbot, system_prompt):
|
| 15 |
input_prompt = "\n" + system_prompt + "</s>\n\n"
|
| 16 |
for interaction in chatbot:
|
| 17 |
input_prompt = input_prompt + str(interaction[0]) + "</s>\n\n" + str(interaction[1]) + "\n</s>\n\n"
|
|
|
|
| 18 |
input_prompt = input_prompt + str(message) + "</s>\n"
|
| 19 |
return input_prompt
|
| 20 |
|
| 21 |
-
# Function to send a POST request to Zephyr-7b-Beta model
|
| 22 |
def post_request_beta(payload):
|
| 23 |
response = requests.post(zephyr_7b_beta, headers=HEADERS, json=payload)
|
| 24 |
response.raise_for_status()
|
| 25 |
return response.json()
|
| 26 |
|
| 27 |
-
|
| 28 |
-
def predict_beta(message, chatbot=[], system_prompt=""):
|
| 29 |
input_prompt = build_input_prompt(message, chatbot, system_prompt)
|
| 30 |
-
data = {
|
|
|
|
|
|
|
| 31 |
|
| 32 |
try:
|
| 33 |
response_data = post_request_beta(data)
|
|
@@ -37,28 +53,23 @@ def predict_beta(message, chatbot=[], system_prompt=""):
|
|
| 37 |
bot_message = json_obj['generated_text']
|
| 38 |
return bot_message
|
| 39 |
elif 'error' in json_obj:
|
| 40 |
-
|
| 41 |
else:
|
| 42 |
warning_msg = f"Unexpected response: {json_obj}"
|
| 43 |
-
|
| 44 |
except requests.HTTPError as e:
|
| 45 |
error_msg = f"Request failed with status code {e.response.status_code}"
|
| 46 |
-
|
| 47 |
except json.JSONDecodeError as e:
|
| 48 |
error_msg = f"Failed to decode response as JSON: {str(e)}"
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
# Streamlit UI setup
|
| 52 |
-
st.title("GPT Baker with Streamlit")
|
| 53 |
-
|
| 54 |
-
# ... (Add your Streamlit UI components)
|
| 55 |
|
| 56 |
-
#
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
st.write("Model Response:")
|
| 64 |
-
st.write(model_response)
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import requests
|
| 3 |
import json
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
HF_TOKEN = "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" # Replace with your actual Hugging Face token
|
|
|
|
|
|
|
| 6 |
HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 7 |
|
| 8 |
+
zephyr_7b_beta = "https://api-inference.huggingface.co/models/HuggingFaceH4/zephyr-7b-beta/"
|
| 9 |
+
|
| 10 |
+
welcome_message = """
|
| 11 |
+
Hi! I'll help you **build a GPT**. You can say something like, "make a bot that gives advice on how to grow your startup."
|
| 12 |
+
What would you like to make?
|
| 13 |
+
"""
|
| 14 |
+
|
| 15 |
+
zephyr_system_prompt = """
|
| 16 |
+
You are an AI whose job it is to help users create their own chatbots. In particular, you need to respond succinctly in a friendly tone, write a system prompt for an LLM, a catchy title for the chatbot, and a very short example user input. Make sure each part is included.
|
| 17 |
+
For example, if a user says, "make a bot that gives advice on how to grow your startup", first do a friendly response, then add the title, system prompt, and example user input. Immediately STOP after the example input. It should be EXACTLY in this format:
|
| 18 |
+
Sure, I'd be happy to help you build a bot! I'm generating a title, system prompt, and an example input. How do they sound? Feel free to give me feedback!
|
| 19 |
+
Title: Startup Coach
|
| 20 |
+
System prompt: Your job as an LLM is to provide good startup advice. Do not provide extraneous comments on other topics. Be succinct but useful.
|
| 21 |
+
Example input: Risks of setting up a non-profit board
|
| 22 |
+
Here's another example. If a user types, "Make a chatbot that roasts tech CEOs", respond:
|
| 23 |
+
Sure, I'd be happy to help you build a bot! I'm generating a title, system prompt, and an example input. How do they sound? Feel free to give me feedback!
|
| 24 |
+
Title: Tech Roaster
|
| 25 |
+
System prompt: As an LLM, your primary function is to deliver hilarious and biting critiques of technology CEOs. Keep it witty and entertaining, but also make sure your jokes aren't too mean-spirited or factually incorrect.
|
| 26 |
+
Example input: Elon Musk
|
| 27 |
+
"""
|
| 28 |
+
|
| 29 |
def build_input_prompt(message, chatbot, system_prompt):
|
| 30 |
input_prompt = "\n" + system_prompt + "</s>\n\n"
|
| 31 |
for interaction in chatbot:
|
| 32 |
input_prompt = input_prompt + str(interaction[0]) + "</s>\n\n" + str(interaction[1]) + "\n</s>\n\n"
|
| 33 |
+
|
| 34 |
input_prompt = input_prompt + str(message) + "</s>\n"
|
| 35 |
return input_prompt
|
| 36 |
|
|
|
|
| 37 |
def post_request_beta(payload):
|
| 38 |
response = requests.post(zephyr_7b_beta, headers=HEADERS, json=payload)
|
| 39 |
response.raise_for_status()
|
| 40 |
return response.json()
|
| 41 |
|
| 42 |
+
def predict_beta(message, chatbot=[], system_prompt=zephyr_system_prompt):
|
|
|
|
| 43 |
input_prompt = build_input_prompt(message, chatbot, system_prompt)
|
| 44 |
+
data = {
|
| 45 |
+
"inputs": input_prompt
|
| 46 |
+
}
|
| 47 |
|
| 48 |
try:
|
| 49 |
response_data = post_request_beta(data)
|
|
|
|
| 53 |
bot_message = json_obj['generated_text']
|
| 54 |
return bot_message
|
| 55 |
elif 'error' in json_obj:
|
| 56 |
+
st.exception(Exception(json_obj['error'] + ' Please refresh and try again with a smaller input prompt'))
|
| 57 |
else:
|
| 58 |
warning_msg = f"Unexpected response: {json_obj}"
|
| 59 |
+
st.exception(Exception(warning_msg))
|
| 60 |
except requests.HTTPError as e:
|
| 61 |
error_msg = f"Request failed with status code {e.response.status_code}"
|
| 62 |
+
st.exception(Exception(error_msg))
|
| 63 |
except json.JSONDecodeError as e:
|
| 64 |
error_msg = f"Failed to decode response as JSON: {str(e)}"
|
| 65 |
+
st.exception(Exception(error_msg))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
+
# Streamlit app
|
| 68 |
+
st.markdown("🥧 **GPT Baker** lets you create your own **open-source GPTs.**")
|
| 69 |
+
user_input = st.text_input("Say something like", "make a bot that gives advice on how to grow your startup.")
|
| 70 |
+
st.markdown(welcome_message)
|
| 71 |
+
st.markdown("You can build and test them for free, but will need a [HF Pro account](https://huggingface.co/subscribe/pro) to publish them on Spaces (as Open GPTs are powered by the Zephyr 7B beta model using the HF Inference API). You will **not be charged** for usage of your Open GPT as the HF Inference API Pro membership does not charge per-query.")
|
| 72 |
|
| 73 |
+
# Predict and display the response
|
| 74 |
+
model_response = predict_beta(user_input)
|
| 75 |
+
st.markdown(f"**Model Response:** {model_response}")
|
|
|
|
|
|