Abs6187 commited on
Commit
baa3d07
·
verified ·
1 Parent(s): f934052

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -30
app.py CHANGED
@@ -1,32 +1,70 @@
1
- # app.py
2
  import os
3
  import gradio as gr
4
- import google.generativeai as genai
5
-
6
- # Configure the Gemini client using an environment variable
7
- genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
8
-
9
- # Create the model and start a stateful chat session
10
- model = genai.GenerativeModel("gemini-2.0-flash")
11
- chat = model.start_chat(history=[])
12
-
13
- # Define the function that Gradio will call on each user message
14
- def respond(message, history):
15
- # Send the user's message to the chat session
16
- response = chat.send_message(message)
17
- # Return only the model's text response
18
- return response.text
19
-
20
- # Build the Gradio UI
21
- # The retry_btn, undo_btn, and clear_btn arguments have been removed
22
- # as this functionality is now integrated into the Chatbot component.
23
- iface = gr.ChatInterface(
24
- fn=respond,
25
- title="Gemini Chatbot",
26
- chatbot=gr.Chatbot(height=600, type="messages"),
27
- textbox=gr.Textbox(placeholder="Ask anything…", container=False, scale=7),
28
- )
29
-
30
- # Launch the application
31
- if __name__ == "__main__":
32
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()