Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,8 +12,6 @@ PX_ENDPOINT_URL = "https://api.perplexity.ai"
|
|
| 12 |
PX_API_KEY = os.getenv('PX_KEY')
|
| 13 |
PASSWORD = os.getenv("PASSWD") # Store the password in an environment variable
|
| 14 |
|
| 15 |
-
DEPLOY_TO_HF = ["deepseek-ai/DeepSeek-V3"]
|
| 16 |
-
|
| 17 |
px_client = OpenAI(base_url=PX_ENDPOINT_URL, api_key=PX_API_KEY)
|
| 18 |
|
| 19 |
def respond(
|
|
@@ -34,17 +32,31 @@ def respond(
|
|
| 34 |
messages.append({"role": "user", "content": message})
|
| 35 |
|
| 36 |
response = ""
|
|
|
|
| 37 |
|
| 38 |
-
|
| 39 |
-
model=AVAILABLE_MODELS[model_choice],
|
| 40 |
messages=messages,
|
| 41 |
max_tokens=max_tokens,
|
| 42 |
temperature=temperature,
|
| 43 |
top_p=top_p,
|
| 44 |
stream=True,
|
| 45 |
-
)
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
yield response
|
| 49 |
|
| 50 |
def check_password(input_password):
|
|
@@ -62,24 +74,36 @@ with gr.Blocks() as demo:
|
|
| 62 |
error_message = gr.Textbox(
|
| 63 |
label="Error", visible=False, interactive=False
|
| 64 |
)
|
| 65 |
-
|
| 66 |
with gr.Column(visible=False) as chat_interface:
|
|
|
|
|
|
|
|
|
|
| 67 |
chat = gr.ChatInterface(
|
| 68 |
respond,
|
| 69 |
-
additional_inputs=[
|
| 70 |
-
|
| 71 |
-
gr.Dropdown(
|
| 72 |
-
choices=list(AVAILABLE_MODELS.keys()),
|
| 73 |
-
value=list(AVAILABLE_MODELS.keys())[0],
|
| 74 |
-
label="Select Model"
|
| 75 |
-
),
|
| 76 |
-
gr.Slider(minimum=1, maximum=30000, value=2048, step=100, label="Max new tokens"),
|
| 77 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 78 |
-
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
| 79 |
-
],
|
| 80 |
)
|
| 81 |
-
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
if __name__ == "__main__":
|
| 85 |
demo.launch(share=True)
|
|
|
|
| 12 |
PX_API_KEY = os.getenv('PX_KEY')
|
| 13 |
PASSWORD = os.getenv("PASSWD") # Store the password in an environment variable
|
| 14 |
|
|
|
|
|
|
|
| 15 |
px_client = OpenAI(base_url=PX_ENDPOINT_URL, api_key=PX_API_KEY)
|
| 16 |
|
| 17 |
def respond(
|
|
|
|
| 32 |
messages.append({"role": "user", "content": message})
|
| 33 |
|
| 34 |
response = ""
|
| 35 |
+
citations = []
|
| 36 |
|
| 37 |
+
stream = px_client.chat.completions.create(
|
| 38 |
+
model=AVAILABLE_MODELS[model_choice],
|
| 39 |
messages=messages,
|
| 40 |
max_tokens=max_tokens,
|
| 41 |
temperature=temperature,
|
| 42 |
top_p=top_p,
|
| 43 |
stream=True,
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
for chunk in stream:
|
| 47 |
+
if "choices" in chunk:
|
| 48 |
+
token = chunk.choices[0].delta.content or ""
|
| 49 |
+
response += token
|
| 50 |
+
yield response # Stream response as it arrives
|
| 51 |
+
if "citations" in chunk:
|
| 52 |
+
citations = chunk["citations"]
|
| 53 |
+
|
| 54 |
+
# Append citations as clickable links
|
| 55 |
+
if citations:
|
| 56 |
+
citation_text = "\n\nSources:\n" + "\n".join(
|
| 57 |
+
[f"[{i+1}] [{url}]({url})" for i, url in enumerate(citations)]
|
| 58 |
+
)
|
| 59 |
+
response += citation_text
|
| 60 |
yield response
|
| 61 |
|
| 62 |
def check_password(input_password):
|
|
|
|
| 74 |
error_message = gr.Textbox(
|
| 75 |
label="Error", visible=False, interactive=False
|
| 76 |
)
|
| 77 |
+
|
| 78 |
with gr.Column(visible=False) as chat_interface:
|
| 79 |
+
system_prompt = gr.Textbox(
|
| 80 |
+
value="You are a helpful assistant.", label="System message"
|
| 81 |
+
)
|
| 82 |
chat = gr.ChatInterface(
|
| 83 |
respond,
|
| 84 |
+
additional_inputs=[],
|
| 85 |
+
height=600 # Increased height for better visibility
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
)
|
| 87 |
+
|
| 88 |
+
with gr.Column():
|
| 89 |
+
model_choice = gr.Dropdown(
|
| 90 |
+
choices=list(AVAILABLE_MODELS.keys()),
|
| 91 |
+
value=list(AVAILABLE_MODELS.keys())[0],
|
| 92 |
+
label="Select Model"
|
| 93 |
+
)
|
| 94 |
+
max_tokens = gr.Slider(
|
| 95 |
+
minimum=1, maximum=30000, value=2048, step=100, label="Max new tokens"
|
| 96 |
+
)
|
| 97 |
+
temperature = gr.Slider(
|
| 98 |
+
minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"
|
| 99 |
+
)
|
| 100 |
+
top_p = gr.Slider(
|
| 101 |
+
minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
submit_button.click(
|
| 105 |
+
check_password, inputs=password_input, outputs=[password_input, chat_interface]
|
| 106 |
+
)
|
| 107 |
|
| 108 |
if __name__ == "__main__":
|
| 109 |
demo.launch(share=True)
|