Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,45 +1,89 @@
|
|
| 1 |
import os
|
| 2 |
-
import
|
| 3 |
import gradio as gr
|
|
|
|
| 4 |
|
| 5 |
-
#
|
| 6 |
api_token = os.getenv("oaikey")
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
top_p=1,
|
| 26 |
-
frequency_penalty=0,
|
| 27 |
-
presence_penalty=0
|
| 28 |
-
)
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
| 41 |
|
| 42 |
-
# Launch the
|
| 43 |
if __name__ == "__main__":
|
| 44 |
-
|
| 45 |
-
|
|
|
|
| 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-4",
|
| 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
|
| 77 |
+
text_output = gr.Textbox(label="Generated Text", lines=8)
|
| 78 |
+
json_output = gr.Code(language="json", label="Full API Response")
|
| 79 |
|
| 80 |
+
# Connect the input components to the function and the output components
|
| 81 |
+
submit_button.click(
|
| 82 |
+
fn=generate_response,
|
| 83 |
+
inputs=[prompt, system_prompt, temperature, top_p, seed],
|
| 84 |
+
outputs=[text_output, json_output]
|
| 85 |
+
)
|
| 86 |
|
| 87 |
+
# Launch the app
|
| 88 |
if __name__ == "__main__":
|
| 89 |
+
app.launch()
|
|
|