ProximileAdmin commited on
Commit
0ac657c
·
verified ·
1 Parent(s): 0b64b3c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -22
app.py CHANGED
@@ -4,38 +4,65 @@ from openai import OpenAI
4
  from typing import List, Tuple
5
 
6
  CLIENTS = {
7
- "perplexity":{"key":os.getenv('PX_KEY'),"endpoint":"https://api.perplexity.ai"},
8
- "hyperbolic":{"key":os.getenv('HYPERBOLIC_XYZ_KEY'),"endpoint":"https://api.hyperbolic.xyz/v1"},
9
- "huggingface":{"key":os.getenv('HF_KEY'),"endpoint":"https://huggingface.co/api/inference-proxy/together"},
10
  }
11
  for client_type in CLIENTS:
12
- CLIENTS[client_type]["client"] = OpenAI(base_url=CLIENTS[client_type]["endpoint"], api_key=CLIENTS[client_type]["key"])
 
 
 
13
 
14
  PASSWORD = os.getenv("PASSWD")
15
 
16
  # Define available models
17
  AVAILABLE_MODELS = {
18
- "Llama3.3-70b-Instruct (Hyperbolic.xyz)": {"model_name":"meta-llama/Llama-3.3-70B-Instruct","type":"hyperbolic"},
19
- "Llama3.1-8b-Instruct (Hyperbolic.xyz)": {"model_name":"meta-llama/Meta-Llama-3.1-8B-Instruct","type":"hyperbolic"},
20
- "DeepSeek V3 (Hyperbolic.xyz)": {"model_name":"deepseek-ai/DeepSeek-V3","type":"hyperbolic"},
21
- "DeepSeek V3 (HuggingFace.co)": {"model_name":"deepseek-ai/DeepSeek-V3","type":"huggingface"},
22
- "Sonar Pro (Perplexity.ai)": {"model_name":"sonar-pro","type":"perplexity"},
23
- "Sonar (Perplexity.ai)": {"model_name":"sonar","type":"perplexity"},
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  }
25
 
26
  def respond(
27
  message: str,
28
  history: List[Tuple[str, str]],
 
29
  system_message: str,
30
  model_choice: str,
31
  max_tokens: int,
32
  temperature: float,
33
  top_p: float,
34
  ):
35
- """Handles chatbot responses with Perplexity AI."""
36
-
 
 
 
 
37
  if model_choice not in AVAILABLE_MODELS:
38
- return "Error: Invalid model selection."
 
39
 
40
  messages = [{"role": "system", "content": system_message}]
41
  for user_msg, assistant_msg in history:
@@ -49,7 +76,7 @@ def respond(
49
  citations = []
50
 
51
  selected_client = CLIENTS[AVAILABLE_MODELS[model_choice]["type"]]["client"]
52
-
53
  try:
54
  stream = selected_client.chat.completions.create(
55
  model=AVAILABLE_MODELS[model_choice]["model_name"],
@@ -68,7 +95,7 @@ def respond(
68
  if hasattr(chunk, "citations") and chunk.citations:
69
  citations = chunk.citations
70
 
71
- # Append citations as clickable links
72
  if citations:
73
  citation_text = "\n\nSources:\n" + "\n".join(
74
  [f"[{i+1}] [{url}]({url})" for i, url in enumerate(citations)]
@@ -79,14 +106,19 @@ def respond(
79
  except Exception as e:
80
  yield f"Error: {str(e)}"
81
 
 
82
  def check_password(input_password):
83
- """Validates the password before showing the chat interface."""
84
  if input_password == PASSWORD:
85
- return gr.update(visible=False), gr.update(visible=True)
 
86
  else:
87
- return gr.update(value="", interactive=True), gr.update(visible=False)
88
 
89
  with gr.Blocks() as demo:
 
 
 
90
  with gr.Column():
91
  password_input = gr.Textbox(
92
  type="password", label="Enter Password", interactive=True
@@ -115,15 +147,19 @@ with gr.Blocks() as demo:
115
  minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"
116
  )
117
 
 
118
  chat = gr.ChatInterface(
119
- respond,
120
  api_name=False,
121
- chatbot=gr.Chatbot(height=400), # Set the desired height here
122
- additional_inputs=[system_prompt, model_choice, max_tokens, temperature, top_p] # Pass extra parameters
123
  )
124
 
 
125
  submit_button.click(
126
- check_password, inputs=password_input, outputs=[password_input, chat_interface]
 
 
127
  )
128
 
129
  if __name__ == "__main__":
 
4
  from typing import List, Tuple
5
 
6
  CLIENTS = {
7
+ "perplexity": {"key": os.getenv('PX_KEY'), "endpoint": "https://api.perplexity.ai"},
8
+ "hyperbolic": {"key": os.getenv('HYPERBOLIC_XYZ_KEY'), "endpoint": "https://api.hyperbolic.xyz/v1"},
9
+ "huggingface": {"key": os.getenv('HF_KEY'), "endpoint": "https://huggingface.co/api/inference-proxy/together"},
10
  }
11
  for client_type in CLIENTS:
12
+ CLIENTS[client_type]["client"] = OpenAI(
13
+ base_url=CLIENTS[client_type]["endpoint"],
14
+ api_key=CLIENTS[client_type]["key"]
15
+ )
16
 
17
  PASSWORD = os.getenv("PASSWD")
18
 
19
  # Define available models
20
  AVAILABLE_MODELS = {
21
+ "Llama3.3-70b-Instruct (Hyperbolic.xyz)": {
22
+ "model_name": "meta-llama/Llama-3.3-70B-Instruct",
23
+ "type": "hyperbolic"
24
+ },
25
+ "Llama3.1-8b-Instruct (Hyperbolic.xyz)": {
26
+ "model_name": "meta-llama/Meta-Llama-3.1-8B-Instruct",
27
+ "type": "hyperbolic"
28
+ },
29
+ "DeepSeek V3 (Hyperbolic.xyz)": {
30
+ "model_name": "deepseek-ai/DeepSeek-V3",
31
+ "type": "hyperbolic"
32
+ },
33
+ "DeepSeek V3 (HuggingFace.co)": {
34
+ "model_name": "deepseek-ai/DeepSeek-V3",
35
+ "type": "huggingface"
36
+ },
37
+ "Sonar Pro (Perplexity.ai)": {
38
+ "model_name": "sonar-pro",
39
+ "type": "perplexity"
40
+ },
41
+ "Sonar (Perplexity.ai)": {
42
+ "model_name": "sonar",
43
+ "type": "perplexity"
44
+ },
45
  }
46
 
47
  def respond(
48
  message: str,
49
  history: List[Tuple[str, str]],
50
+ session_password: str, # added parameter from session state
51
  system_message: str,
52
  model_choice: str,
53
  max_tokens: int,
54
  temperature: float,
55
  top_p: float,
56
  ):
57
+ """Handles chatbot responses with password re-checking."""
58
+ # Re-check the session password on every message
59
+ if session_password != PASSWORD:
60
+ yield "Error: Invalid session password. Please refresh the page and enter the correct password."
61
+ return
62
+
63
  if model_choice not in AVAILABLE_MODELS:
64
+ yield "Error: Invalid model selection."
65
+ return
66
 
67
  messages = [{"role": "system", "content": system_message}]
68
  for user_msg, assistant_msg in history:
 
76
  citations = []
77
 
78
  selected_client = CLIENTS[AVAILABLE_MODELS[model_choice]["type"]]["client"]
79
+
80
  try:
81
  stream = selected_client.chat.completions.create(
82
  model=AVAILABLE_MODELS[model_choice]["model_name"],
 
95
  if hasattr(chunk, "citations") and chunk.citations:
96
  citations = chunk.citations
97
 
98
+ # Append citations as clickable links, if any
99
  if citations:
100
  citation_text = "\n\nSources:\n" + "\n".join(
101
  [f"[{i+1}] [{url}]({url})" for i, url in enumerate(citations)]
 
106
  except Exception as e:
107
  yield f"Error: {str(e)}"
108
 
109
+
110
  def check_password(input_password):
111
+ """Validates the password and, if valid, stores it in session state."""
112
  if input_password == PASSWORD:
113
+ # Return the password to store in the session state.
114
+ return gr.update(visible=False), gr.update(visible=True), input_password
115
  else:
116
+ return gr.update(value="", interactive=True), gr.update(visible=False), ""
117
 
118
  with gr.Blocks() as demo:
119
+ # A hidden state component to store the session password
120
+ session_password = gr.State("")
121
+
122
  with gr.Column():
123
  password_input = gr.Textbox(
124
  type="password", label="Enter Password", interactive=True
 
147
  minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"
148
  )
149
 
150
+ # Note: The session_password is now passed as an additional input to the chat function.
151
  chat = gr.ChatInterface(
152
+ fn=respond,
153
  api_name=False,
154
+ chatbot=gr.Chatbot(height=400), # Set desired height here
155
+ additional_inputs=[session_password, system_prompt, model_choice, max_tokens, temperature, top_p]
156
  )
157
 
158
+ # Now, the submit_button click updates three outputs: the password_input, chat_interface visibility, and session_password state.
159
  submit_button.click(
160
+ fn=check_password,
161
+ inputs=password_input,
162
+ outputs=[password_input, chat_interface, session_password]
163
  )
164
 
165
  if __name__ == "__main__":