Abs6187 commited on
Commit
cc40246
·
verified ·
1 Parent(s): c7dbe9c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -47
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
- # 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
- self.locked = False
 
 
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}\\nUser: {prompt}\\nGPT-5:"
25
  data = {
26
  "prompt": full_prompt,
27
- "max_tokens": 150
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
- # Write the model files to disk
44
- for filename, content in model_files.items():
45
- Path(filename).write_text(content.strip())
46
 
47
- # Import the load_model function dynamically
48
- spec = importlib.util.spec_from_file_location("gpt5_utils", "gpt5_utils.py")
49
- gpt5_utils = importlib.util.module_from_spec(spec)
50
- spec.loader.exec_module(gpt5_utils)
51
 
52
- # Get API key from environment
 
 
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
- # Load the GPT5 model
58
- model = gpt5_utils.load_model(api_key)
59
 
60
- # Response generation function
61
- def generate_response(prompt):
62
- if len(prompt) > 1:
63
- return "Error: Prompt must be exactly 1 character long."
64
- return model.generate_response(prompt)
65
 
66
- # Create Gradio interface
67
- with gr.Blocks() as demo:
68
- gr.Markdown("# GPT5 Model Interface (One Prompt Only)")
 
 
 
 
 
69
  with gr.Row():
70
- with gr.Column():
71
- prompt = gr.Textbox(label="Enter your prompt (1 character only)", placeholder="Enter a single character")
72
- generate_btn = gr.Button("Generate Response")
73
- output = gr.Textbox(label="Generated Response", interactive=False)
 
 
 
 
 
74
 
75
- generate_btn.click(generate_response, inputs=prompt, outputs=output)
 
 
 
 
 
 
 
 
 
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
+