Update app.py
Browse files
app.py
CHANGED
@@ -1,32 +1,70 @@
|
|
1 |
-
# app.py
|
2 |
import os
|
3 |
import gradio as gr
|
4 |
-
import
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
+
import requests
|
4 |
+
import importlib.util
|
5 |
+
from pathlib import Path
|
6 |
+
|
7 |
+
# Define the model files content
|
8 |
+
model_files = {
|
9 |
+
"gpt5_model.py": """
|
10 |
+
class GPT5Model:
|
11 |
+
def __init__(self, api_key):
|
12 |
+
self.api_key = api_key
|
13 |
+
self.system_prompt = "You are GPT-5, the most advanced AI model available. Answer accurately, intelligently, and helpfully."
|
14 |
+
|
15 |
+
def generate_response(self, prompt):
|
16 |
+
headers = {
|
17 |
+
"Authorization": f"Bearer {self.api_key}",
|
18 |
+
"Content-Type": "application/json"
|
19 |
+
}
|
20 |
+
full_prompt = f"{self.system_prompt}\\nUser: {prompt}\\nGPT-5:"
|
21 |
+
data = {
|
22 |
+
"prompt": full_prompt,
|
23 |
+
"max_tokens": 150
|
24 |
+
}
|
25 |
+
import requests
|
26 |
+
response = requests.post("https://api.pplx.ai/v1/generate", json=data, headers=headers)
|
27 |
+
return response.json()["choices"][0]["text"].strip()
|
28 |
+
""",
|
29 |
+
"gpt5_utils.py": """
|
30 |
+
def load_model(api_key):
|
31 |
+
from gpt5_model import GPT5Model
|
32 |
+
return GPT5Model(api_key)
|
33 |
+
"""
|
34 |
+
}
|
35 |
+
|
36 |
+
# Write the model files to disk
|
37 |
+
for filename, content in model_files.items():
|
38 |
+
Path(filename).write_text(content.strip())
|
39 |
+
|
40 |
+
# Import the load_model function dynamically
|
41 |
+
spec = importlib.util.spec_from_file_location("gpt5_utils", "gpt5_utils.py")
|
42 |
+
gpt5_utils = importlib.util.module_from_spec(spec)
|
43 |
+
spec.loader.exec_module(gpt5_utils)
|
44 |
+
|
45 |
+
# Get API key from environment
|
46 |
+
api_key = os.getenv("PPLX_API_KEY")
|
47 |
+
if not api_key:
|
48 |
+
raise ValueError("API key not found. Please set PPLX_API_KEY environment variable.")
|
49 |
+
|
50 |
+
# Load the GPT5 model
|
51 |
+
model = gpt5_utils.load_model(api_key)
|
52 |
+
|
53 |
+
# Response generation function
|
54 |
+
def generate_response(prompt):
|
55 |
+
if len(prompt) > 1:
|
56 |
+
return "Error: Prompt must be exactly 1 character long."
|
57 |
+
return model.generate_response(prompt)
|
58 |
+
|
59 |
+
# Create Gradio interface
|
60 |
+
with gr.Blocks() as demo:
|
61 |
+
gr.Markdown("# GPT5 Model Interface")
|
62 |
+
with gr.Row():
|
63 |
+
with gr.Column():
|
64 |
+
prompt = gr.Textbox(label="Enter your prompt (1 character only)", placeholder="Enter a single character")
|
65 |
+
generate_btn = gr.Button("Generate Response")
|
66 |
+
output = gr.Textbox(label="Generated Response")
|
67 |
+
|
68 |
+
generate_btn.click(generate_response, inputs=prompt, outputs=output)
|
69 |
+
|
70 |
+
demo.launch()
|