Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,78 +1,5 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 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." What would you like to make?
|
| 12 |
-
"""
|
| 13 |
-
|
| 14 |
-
zephyr_system_prompt = """
|
| 15 |
-
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.
|
| 16 |
-
|
| 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 |
-
|
| 19 |
-
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!
|
| 20 |
-
Title: Startup Coach
|
| 21 |
-
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.
|
| 22 |
-
Example input: Risks of setting up a non-profit board
|
| 23 |
-
|
| 24 |
-
Here's another example. If a user types, "Make a chatbot that roasts tech CEOs", respond:
|
| 25 |
-
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!
|
| 26 |
-
Title: Tech Roaster
|
| 27 |
-
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.
|
| 28 |
-
Example input: Elon Musk
|
| 29 |
-
"""
|
| 30 |
-
|
| 31 |
-
def build_input_prompt(message, chatbot, system_prompt):
|
| 32 |
-
input_prompt = "\n" + system_prompt + "</s>\n\n"
|
| 33 |
-
for interaction in chatbot:
|
| 34 |
-
input_prompt = input_prompt + str(interaction[0]) + "</s>\n\n" + str(interaction[1]) + "\n</s>\n\n"
|
| 35 |
-
|
| 36 |
-
input_prompt = input_prompt + str(message) + "</s>\n"
|
| 37 |
-
return input_prompt
|
| 38 |
-
|
| 39 |
-
def post_request_beta(payload):
|
| 40 |
-
response = requests.post(zephyr_7b_beta, headers=HEADERS, json=payload)
|
| 41 |
-
response.raise_for_status()
|
| 42 |
-
return response.json()
|
| 43 |
-
|
| 44 |
-
def predict_beta(message, chatbot=[], system_prompt=zephyr_system_prompt):
|
| 45 |
-
input_prompt = build_input_prompt(message, chatbot, system_prompt)
|
| 46 |
-
data = {
|
| 47 |
-
"inputs": input_prompt
|
| 48 |
-
}
|
| 49 |
-
|
| 50 |
-
try:
|
| 51 |
-
response_data = post_request_beta(data)
|
| 52 |
-
json_obj = response_data[0]
|
| 53 |
-
|
| 54 |
-
if 'generated_text' in json_obj and len(json_obj['generated_text']) > 0:
|
| 55 |
-
bot_message = json_obj['generated_text']
|
| 56 |
-
return bot_message
|
| 57 |
-
elif 'error' in json_obj:
|
| 58 |
-
st.exception(Exception(json_obj['error'] + ' Please refresh and try again with a smaller input prompt'))
|
| 59 |
-
else:
|
| 60 |
-
warning_msg = f"Unexpected response: {json_obj}"
|
| 61 |
-
st.exception(Exception(warning_msg))
|
| 62 |
-
except requests.HTTPError as e:
|
| 63 |
-
error_msg = f"Request failed with status code {e.response.status_code}"
|
| 64 |
-
st.exception(Exception(error_msg))
|
| 65 |
-
except json.JSONDecodeError as e:
|
| 66 |
-
error_msg = f"Failed to decode response as JSON: {str(e)}"
|
| 67 |
-
st.exception(Exception(error_msg))
|
| 68 |
-
|
| 69 |
-
# Streamlit app
|
| 70 |
-
st.markdown("🥧 **GPT Baker** lets you create your own **open-source GPTs.**")
|
| 71 |
-
user_input = st.text_input("Say something like", "make a bot that gives advice on how to grow your startup.")
|
| 72 |
-
st.markdown(welcome_message)
|
| 73 |
-
st.markdown("You can build and test them for free,")
|
| 74 |
-
|
| 75 |
-
# Predict and display the response
|
| 76 |
-
model_response = predict_beta(user_input)
|
| 77 |
-
st.markdown(f"**Model Response:** {model_response}")
|
| 78 |
|
|
|
|
|
|
|
|
|
| 1 |
+
from openai import OpenAI
|
| 2 |
+
client = OpenAI()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
my_assistant = client.beta.assistants.retrieve("asst_abc123")
|
| 5 |
+
print(my_assistant)
|