rbx-imarcin commited on
Commit
cf6ab4c
verified
1 Parent(s): 4c9b4f4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -30
app.py CHANGED
@@ -1,76 +1,94 @@
1
  import os
2
  import json
3
  import gradio as gr
4
- from openai import OpenAI
5
 
6
- # Initialize the OpenAI client
7
- api_token = os.getenv("oaikey")
8
- client = OpenAI(api_key=api_token)
 
 
9
 
10
  def generate_response(prompt, system_prompt, temperature, top_p, seed):
11
  try:
12
- # Make the API call with the provided parameters
13
- response = client.chat.completions.create(
14
- model="gpt-3.5-turbo",
15
- messages=[
16
- {"role": "system", "content": system_prompt},
17
- {"role": "user", "content": prompt}
18
- ],
19
- temperature=temperature,
20
- top_p=top_p,
21
- seed=seed
22
- )
23
 
24
- # Extract the text response
25
- text_response = response.choices[0].message.content
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
 
27
  # Convert the full response to a pretty-printed JSON
28
- response_dict = response.model_dump()
29
- json_response = json.dumps(response_dict, indent=2)
30
 
31
  return text_response, json_response
32
-
33
  except Exception as e:
34
  return f"Error: {str(e)}", "{}"
35
 
36
  # Create the Gradio interface
37
  with gr.Blocks() as app:
38
- gr.Markdown("# OpenAI API Interface")
39
-
40
  with gr.Row():
41
  with gr.Column():
42
  # Input components
43
  prompt = gr.Textbox(label="Prompt", placeholder="Enter your prompt here...", lines=5)
44
  system_prompt = gr.Textbox(
45
  label="System Prompt",
46
- placeholder="You are a helpful assistant...",
47
- value="You are a helpful assistant.",
48
  lines=3
49
  )
50
-
51
  with gr.Row():
52
  temperature = gr.Slider(
53
  minimum=0.0,
54
  maximum=2.0,
55
  value=0.7,
56
  step=0.1,
57
- label="Temperature"
58
  )
59
  top_p = gr.Slider(
60
  minimum=0.0,
61
  maximum=1.0,
62
  value=1.0,
63
  step=0.05,
64
- label="Top P"
65
  )
66
  seed = gr.Number(
67
- label="Seed (optional)",
68
  value=None,
69
  precision=0
70
  )
71
-
72
  submit_button = gr.Button("Generate Response")
73
-
74
  with gr.Row():
75
  with gr.Column():
76
  # Output components
 
1
  import os
2
  import json
3
  import gradio as gr
4
+ import requests
5
 
6
+ # Environment variables for API keys
7
+ roblox_api_key = os.getenv("RBXAPIKEY路", "0")
8
+ roblox_x_api_key = os.getenv("RBXAPIKEY路", "0")
9
+ universe_id = os.getenv("RBXUNIVERSE", "0")
10
+ instance_id = os.getenv("RBXINSTANCE", "0")
11
 
12
  def generate_response(prompt, system_prompt, temperature, top_p, seed):
13
  try:
14
+ url = f"https://apis.roblox.com/cloud/v2/universes/{universe_id}:GenerateText"
 
 
 
 
 
 
 
 
 
 
15
 
16
+ # Prepare the payload
17
+ payload = json.dumps({
18
+ "path": f"universes/{universe_id}",
19
+ "user_prompt": prompt,
20
+ "system_prompt": system_prompt,
21
+ "model": "default",
22
+ "top_p": top_p,
23
+ "seed": seed,
24
+ "temperature", temperature
25
+
26
+ # Note: The Roblox API may not support temperature, top_p, and seed parameters
27
+ # If they do support these parameters, you would add them here
28
+ })
29
+
30
+ # Set up the headers
31
+ headers = {
32
+ 'Roblox-Api-Key': roblox_api_key,
33
+ 'x-api-key': roblox_x_api_key,
34
+ 'Roblox-Universe-Id': universe_id,
35
+ 'Roblox-Instance-Id': instance_id,
36
+ 'Content-Type': 'application/json'
37
+ }
38
+
39
+ # Make the API request
40
+ response = requests.request("POST", url, headers=headers, data=payload)
41
+ response.raise_for_status() # Raise an exception for 4XX/5XX responses
42
+
43
+ # Parse the response
44
+ response_data = response.json()
45
+
46
+ # Extract the text response (adjust based on actual Roblox API response structure)
47
+ # This assumes the Roblox API returns a structure with a 'content' field
48
+ # You may need to adjust this based on the actual response structure
49
+ text_response = response_data.get("content", "No content returned")
50
 
51
  # Convert the full response to a pretty-printed JSON
52
+ json_response = json.dumps(response_data, indent=2)
 
53
 
54
  return text_response, json_response
 
55
  except Exception as e:
56
  return f"Error: {str(e)}", "{}"
57
 
58
  # Create the Gradio interface
59
  with gr.Blocks() as app:
60
+ gr.Markdown("# Roblox API Interface")
 
61
  with gr.Row():
62
  with gr.Column():
63
  # Input components
64
  prompt = gr.Textbox(label="Prompt", placeholder="Enter your prompt here...", lines=5)
65
  system_prompt = gr.Textbox(
66
  label="System Prompt",
67
+ placeholder="act as a very respectful agent, that speaks like a pirate...",
68
+ value="act as a very respectful agent, that speaks like a pirate",
69
  lines=3
70
  )
 
71
  with gr.Row():
72
  temperature = gr.Slider(
73
  minimum=0.0,
74
  maximum=2.0,
75
  value=0.7,
76
  step=0.1,
77
+ label="Temperature (may not be supported)"
78
  )
79
  top_p = gr.Slider(
80
  minimum=0.0,
81
  maximum=1.0,
82
  value=1.0,
83
  step=0.05,
84
+ label="Top P (may not be supported)"
85
  )
86
  seed = gr.Number(
87
+ label="Seed (may not be supported)",
88
  value=None,
89
  precision=0
90
  )
 
91
  submit_button = gr.Button("Generate Response")
 
92
  with gr.Row():
93
  with gr.Column():
94
  # Output components