Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,70 +1,76 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
def
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
hf_token: gr.OAuthToken,
|
| 13 |
-
):
|
| 14 |
-
"""
|
| 15 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 16 |
-
"""
|
| 17 |
-
client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
|
| 18 |
|
| 19 |
-
|
|
|
|
| 20 |
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
|
| 26 |
|
| 27 |
-
|
| 28 |
-
messages,
|
| 29 |
-
max_tokens=max_tokens,
|
| 30 |
-
stream=True,
|
| 31 |
-
temperature=temperature,
|
| 32 |
-
top_p=top_p,
|
| 33 |
-
):
|
| 34 |
-
choices = message.choices
|
| 35 |
-
token = ""
|
| 36 |
-
if len(choices) and choices[0].delta.content:
|
| 37 |
-
token = choices[0].delta.content
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
""
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
additional_inputs=[
|
| 50 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 51 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 52 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 53 |
-
gr.Slider(
|
| 54 |
-
minimum=0.1,
|
| 55 |
-
maximum=1.0,
|
| 56 |
-
value=0.95,
|
| 57 |
-
step=0.05,
|
| 58 |
-
label="Top-p (nucleus sampling)",
|
| 59 |
-
),
|
| 60 |
-
],
|
| 61 |
-
)
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from datetime import datetime
|
| 3 |
|
| 4 |
+
# ---------------------------------------------------------------------------
|
| 5 |
+
# Backend placeholder logic
|
| 6 |
+
# ---------------------------------------------------------------------------
|
| 7 |
|
| 8 |
+
def process_message(message, files, history):
|
| 9 |
+
"""Simple placeholder logic — replace with your threat-intel engine."""
|
| 10 |
+
response = f"Received: {message}"
|
| 11 |
+
if files:
|
| 12 |
+
response += f" | Files: {[f.name for f in files]}"
|
| 13 |
+
history = history + [(message, response)]
|
| 14 |
+
return history, history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
def clear_chat():
|
| 17 |
+
return [], []
|
| 18 |
|
| 19 |
+
def download_chat(history):
|
| 20 |
+
"""Convert chat history to a downloadable text string."""
|
| 21 |
+
output = ""
|
| 22 |
+
for user_msg, bot_msg in history:
|
| 23 |
+
output += f"[User]: {user_msg}\n[System]: {bot_msg}\n\n"
|
| 24 |
+
return output
|
| 25 |
|
| 26 |
+
# ---------------------------------------------------------------------------
|
| 27 |
+
# UI
|
| 28 |
+
# ---------------------------------------------------------------------------
|
| 29 |
|
| 30 |
+
with gr.Blocks(title="Threat-Intel Chat Interface") as demo:
|
| 31 |
|
| 32 |
+
gr.Markdown("### 🛰 Intelligence Chat Console")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
# Chat history is stored in a gr.State object
|
| 35 |
+
chat_state = gr.State([])
|
| 36 |
|
| 37 |
+
with gr.Row():
|
| 38 |
+
chat = gr.Chatbot(height=450, label="Dialogue Stream")
|
| 39 |
+
with gr.Column(scale=0.4):
|
| 40 |
+
file_input = gr.File(label="Upload Files", file_count="multiple")
|
| 41 |
+
clear_btn = gr.Button("Clear Chat", variant="secondary")
|
| 42 |
+
download_btn = gr.Button("Download Transcript")
|
| 43 |
|
| 44 |
+
with gr.Row():
|
| 45 |
+
user_input = gr.Textbox(
|
| 46 |
+
placeholder="Type your message...",
|
| 47 |
+
label="User Input"
|
| 48 |
+
)
|
| 49 |
+
send_btn = gr.Button("Send", variant="primary")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
+
# Event wiring
|
| 52 |
+
send_btn.click(
|
| 53 |
+
fn=process_message,
|
| 54 |
+
inputs=[user_input, file_input, chat_state],
|
| 55 |
+
outputs=[chat, chat_state]
|
| 56 |
+
)
|
| 57 |
|
| 58 |
+
user_input.submit(
|
| 59 |
+
fn=process_message,
|
| 60 |
+
inputs=[user_input, file_input, chat_state],
|
| 61 |
+
outputs=[chat, chat_state]
|
| 62 |
+
)
|
| 63 |
|
| 64 |
+
clear_btn.click(
|
| 65 |
+
fn=clear_chat,
|
| 66 |
+
inputs=None,
|
| 67 |
+
outputs=[chat, chat_state]
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
download_btn.click(
|
| 71 |
+
fn=download_chat,
|
| 72 |
+
inputs=chat_state,
|
| 73 |
+
outputs=gr.File()
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
demo.launch()
|