Spaces:
Sleeping
Sleeping
File size: 3,484 Bytes
cd3d012 d4a76e1 2abdd24 cf6ab4c cd3d012 cf6ab4c eee4b74 cd3d012 d4a76e1 cf6ab4c d4a76e1 cf6ab4c 69b26c9 cf6ab4c 4f11d3f cf6ab4c 69b26c9 bec4723 d4a76e1 cf6ab4c d4a76e1 92c446a d4a76e1 cf6ab4c d4a76e1 eee4b74 d4a76e1 eee4b74 d4a76e1 69b26c9 d4a76e1 69b26c9 d4a76e1 2abdd24 d4a76e1 4e5ea86 d4a76e1 4e5ea86 d4a76e1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
import os
import json
import gradio as gr
import requests
# Environment variables for API keys
roblox_api_key = os.getenv("RBXAPIKEY")
roblox_x_api_key = os.getenv("RBXAPIKEY")
universe_id = os.getenv("RBXUNIVERSE")
instance_id = os.getenv("RBXINSTANCE")
def generate_response(prompt, system_prompt, temperature, top_p, seed):
try:
url = f"https://apis.roblox.com/cloud/v2/universes/{universe_id}:GenerateText"
# Prepare the payload
payload = json.dumps({
"path": f"universes/{universe_id}",
"user_prompt": prompt,
"system_prompt": system_prompt,
"model": "default",
"top_p": top_p,
"seed": seed,
"temperature": temperature,
})
# Set up the headers
headers = {
'Roblox-Api-Key': roblox_api_key,
'x-api-key': roblox_x_api_key,
'Roblox-Universe-Id': universe_id,
'Roblox-Instance-Id': instance_id,
'Content-Type': 'application/json'
}
# Make the API request
response = requests.request("POST", url, headers=headers, data=payload)
print(response)
response.raise_for_status() # Raise an exception for 4XX/5XX responses
response_data = response.json()
text_response = response_data.get("generatedText", "No content returned")
# Convert the full response to a pretty-printed JSON
json_response = json.dumps(response_data, indent=2)
return text_response, json_response
except Exception as e:
return f"Error: {str(e)}", "{}"
# Create the Gradio interface
with gr.Blocks() as app:
gr.Markdown("# Roblox Safe Generation API (Generate Text)")
with gr.Row():
with gr.Column():
# Input components
prompt = gr.Textbox(label="Prompt", placeholder="Enter your prompt here...", lines=5)
system_prompt = gr.Textbox(
label="System Prompt",
placeholder="act as a very respectful agent, that speaks like a pirate...",
value="act as a very respectful agent, that speaks like a pirate",
lines=3
)
with gr.Row():
temperature = gr.Slider(
minimum=0.0,
maximum=1.0,
value=0.7,
step=0.1,
label="Temperature"
)
top_p = gr.Slider(
minimum=0.0,
maximum=1.0,
value=1.0,
step=0.05,
label="Top P"
)
seed = gr.Number(
label="Seed",
value=None,
precision=0
)
submit_button = gr.Button("Generate Response")
with gr.Row():
with gr.Column():
# Output components
text_output = gr.Textbox(label="Generated Text", lines=8)
json_output = gr.Code(language="json", label="Full API Response")
# Connect the input components to the function and the output components
submit_button.click(
fn=generate_response,
inputs=[prompt, system_prompt, temperature, top_p, seed],
outputs=[text_output, json_output]
)
# Launch the app
if __name__ == "__main__":
app.launch() |