rbx-imarcin's picture
Update app.py
bec4723 verified
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()