S-Dreamer commited on
Commit
e73e89e
·
verified ·
1 Parent(s): 68651b9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -56
app.py CHANGED
@@ -1,70 +1,76 @@
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
 
 
 
 
4
 
5
- def respond(
6
- message,
7
- history: list[dict[str, str]],
8
- system_message,
9
- max_tokens,
10
- temperature,
11
- top_p,
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
- messages = [{"role": "system", "content": system_message}]
 
20
 
21
- messages.extend(history)
 
 
 
 
 
22
 
23
- messages.append({"role": "user", "content": message})
 
 
24
 
25
- response = ""
26
 
27
- for message in client.chat_completion(
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
- response += token
40
- yield response
41
 
 
 
 
 
 
 
42
 
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
- chatbot = gr.ChatInterface(
47
- respond,
48
- type="messages",
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
- with gr.Blocks() as demo:
64
- with gr.Sidebar():
65
- gr.LoginButton()
66
- chatbot.render()
 
 
67
 
 
 
 
 
 
68
 
69
- if __name__ == "__main__":
70
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
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()