Update app.py
Browse files
app.py
CHANGED
@@ -1,77 +1,82 @@
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
import requests
|
4 |
-
import importlib.util
|
5 |
-
from pathlib import Path
|
6 |
|
7 |
-
#
|
8 |
-
|
9 |
-
|
10 |
class GPT5Model:
|
11 |
def __init__(self, api_key):
|
12 |
self.api_key = api_key
|
13 |
-
self.system_prompt =
|
14 |
-
|
|
|
|
|
15 |
|
16 |
def generate_response(self, prompt):
|
17 |
-
if self.locked:
|
18 |
-
return "Access locked. Only one prompt is allowed per session."
|
19 |
-
|
20 |
headers = {
|
21 |
"Authorization": f"Bearer {self.api_key}",
|
22 |
"Content-Type": "application/json"
|
23 |
}
|
24 |
-
full_prompt = f"{self.system_prompt}
|
25 |
data = {
|
26 |
"prompt": full_prompt,
|
27 |
-
"max_tokens":
|
28 |
}
|
29 |
-
import requests
|
30 |
response = requests.post("https://api.pplx.ai/v1/generate", json=data, headers=headers)
|
31 |
-
|
32 |
-
# Lock after first request
|
33 |
-
self.locked = True
|
34 |
-
return response.json()["choices"][0]["text"].strip()
|
35 |
-
""",
|
36 |
-
"gpt5_utils.py": """
|
37 |
-
def load_model(api_key):
|
38 |
-
from gpt5_model import GPT5Model
|
39 |
-
return GPT5Model(api_key)
|
40 |
-
"""
|
41 |
-
}
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
Path(filename).write_text(content.strip())
|
46 |
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
|
52 |
-
#
|
|
|
|
|
53 |
api_key = os.getenv("PPLX_API_KEY")
|
54 |
if not api_key:
|
55 |
raise ValueError("API key not found. Please set PPLX_API_KEY environment variable.")
|
56 |
|
57 |
-
|
58 |
-
model = gpt5_utils.load_model(api_key)
|
59 |
|
60 |
-
#
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
return model.generate_response(
|
65 |
|
66 |
-
#
|
67 |
-
|
68 |
-
|
|
|
|
|
|
|
|
|
|
|
69 |
with gr.Row():
|
70 |
-
with gr.Column():
|
71 |
-
|
72 |
-
|
73 |
-
|
|
|
|
|
|
|
|
|
|
|
74 |
|
75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
|
77 |
demo.launch()
|
|
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
import requests
|
|
|
|
|
4 |
|
5 |
+
# -----------------------------
|
6 |
+
# GPT5 Model Class
|
7 |
+
# -----------------------------
|
8 |
class GPT5Model:
|
9 |
def __init__(self, api_key):
|
10 |
self.api_key = api_key
|
11 |
+
self.system_prompt = (
|
12 |
+
"You are GPT-5, the most advanced AI model available. "
|
13 |
+
"Answer accurately, intelligently, and helpfully."
|
14 |
+
)
|
15 |
|
16 |
def generate_response(self, prompt):
|
|
|
|
|
|
|
17 |
headers = {
|
18 |
"Authorization": f"Bearer {self.api_key}",
|
19 |
"Content-Type": "application/json"
|
20 |
}
|
21 |
+
full_prompt = f"{self.system_prompt}\nUser: {prompt}\nGPT-5:"
|
22 |
data = {
|
23 |
"prompt": full_prompt,
|
24 |
+
"max_tokens": 500
|
25 |
}
|
|
|
26 |
response = requests.post("https://api.pplx.ai/v1/generate", json=data, headers=headers)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
+
if response.status_code != 200:
|
29 |
+
return f"Error: {response.text}"
|
|
|
30 |
|
31 |
+
try:
|
32 |
+
return response.json()["choices"][0]["text"].strip()
|
33 |
+
except Exception as e:
|
34 |
+
return f"Error parsing response: {e}"
|
35 |
|
36 |
+
# -----------------------------
|
37 |
+
# Load API key
|
38 |
+
# -----------------------------
|
39 |
api_key = os.getenv("PPLX_API_KEY")
|
40 |
if not api_key:
|
41 |
raise ValueError("API key not found. Please set PPLX_API_KEY environment variable.")
|
42 |
|
43 |
+
model = GPT5Model(api_key)
|
|
|
44 |
|
45 |
+
# -----------------------------
|
46 |
+
# UI Function
|
47 |
+
# -----------------------------
|
48 |
+
def chat_with_gpt5(user_input):
|
49 |
+
return model.generate_response(user_input)
|
50 |
|
51 |
+
# -----------------------------
|
52 |
+
# Build Better UI
|
53 |
+
# -----------------------------
|
54 |
+
with gr.Blocks(css="""
|
55 |
+
#title {text-align: center; font-size: 28px; font-weight: bold;}
|
56 |
+
#footer {text-align: center; font-size: 14px; color: gray;}
|
57 |
+
""") as demo:
|
58 |
+
gr.Markdown("<div id='title'>🚀 GPT-5 Model Interface</div>")
|
59 |
with gr.Row():
|
60 |
+
with gr.Column(scale=1):
|
61 |
+
chatbot = gr.Chatbot(height=400, label="GPT-5 Conversation")
|
62 |
+
with gr.Row():
|
63 |
+
txt = gr.Textbox(
|
64 |
+
show_label=False,
|
65 |
+
placeholder="Type your message here...",
|
66 |
+
container=False
|
67 |
+
)
|
68 |
+
send_btn = gr.Button("Send", variant="primary")
|
69 |
|
70 |
+
gr.Markdown("<div id='footer'>Powered by GPT-5 API Simulation | © 2025</div>")
|
71 |
+
|
72 |
+
# Handle chat logic
|
73 |
+
def respond(message, chat_history):
|
74 |
+
bot_reply = chat_with_gpt5(message)
|
75 |
+
chat_history.append(("You: " + message, "GPT-5: " + bot_reply))
|
76 |
+
return "", chat_history
|
77 |
+
|
78 |
+
send_btn.click(respond, [txt, chatbot], [txt, chatbot])
|
79 |
+
txt.submit(respond, [txt, chatbot], [txt, chatbot])
|
80 |
|
81 |
demo.launch()
|
82 |
+
|