SBK commited on
Commit
9885321
·
verified ·
1 Parent(s): 4f5c655

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -18
app.py CHANGED
@@ -6,9 +6,12 @@ import threading
6
  # === Model loading ===
7
  model_path = "SBK/sbk-llm-1" # Using your HF model
8
  tokenizer = AutoTokenizer.from_pretrained(model_path)
9
- model = AutoModelForCausalLM.from_pretrained(model_path, torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32)
 
 
 
 
10
  device = "cuda" if torch.cuda.is_available() else "cpu"
11
- model.to(device)
12
 
13
  # === System prompt / default behavior ===
14
  SYSTEM_PROMPT = """You are a helpful, honest, and factual assistant trained to answer only about me *Saptarshi Bhattacharya*. You were fine-tuned on factual data derived from his work, projects, skills, internships, and engineering experiences.
@@ -24,7 +27,7 @@ Your job is to help users understand what Saptarshi has done, what he's good at,
24
  Your goal is to represent him truthfully and make his work accessible and understandable to potential collaborators or employers, without overselling or faking.
25
  """
26
 
27
- BLOCKED_KEYWORDS = ["kill", "harm", "violence", "bomb", "suicide"] # simple guardrail
28
  MAX_TOKENS = 512
29
 
30
  # === Streaming generation ===
@@ -64,33 +67,55 @@ def generate_response(history, system_prompt):
64
  yield partial_message
65
 
66
  # === Gradio interface ===
67
- with gr.Blocks() as demo:
68
- gr.Markdown("## 🧠 Chat with SBK LLM")
69
- system_prompt = gr.Textbox(label="System Prompt", value=SYSTEM_PROMPT, lines=2)
70
- chatbot = gr.Chatbot() # Using default tuple format
71
- msg = gr.Textbox(label="Your Message", placeholder="Ask me anything...", lines=1)
72
- clear = gr.Button("Clear")
73
-
74
- history = gr.State([]) # memory for session
 
 
 
 
 
 
75
 
76
  def respond(user_message, chat_history, system_prompt):
77
- # Append user message to history
78
- chat_history.append((user_message, ""))
79
 
80
- # Generate response
81
  full_response = ""
82
  for response in generate_response(chat_history, system_prompt):
83
  full_response = response
84
  chat_history[-1] = (user_message, full_response)
85
  yield chat_history
86
 
87
- return "", chat_history
88
 
 
89
  msg.submit(
90
  respond,
91
  [msg, chatbot, system_prompt],
92
- [chatbot, history],
 
 
 
 
 
 
 
 
 
 
 
 
93
  )
94
- clear.click(lambda: ([], []), outputs=[chatbot, history])
95
 
96
- demo.queue().launch(share=True)
 
 
 
 
 
 
 
6
  # === Model loading ===
7
  model_path = "SBK/sbk-llm-1" # Using your HF model
8
  tokenizer = AutoTokenizer.from_pretrained(model_path)
9
+ model = AutoModelForCausalLM.from_pretrained(
10
+ model_path,
11
+ torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
12
+ device_map="auto"
13
+ )
14
  device = "cuda" if torch.cuda.is_available() else "cpu"
 
15
 
16
  # === System prompt / default behavior ===
17
  SYSTEM_PROMPT = """You are a helpful, honest, and factual assistant trained to answer only about me *Saptarshi Bhattacharya*. You were fine-tuned on factual data derived from his work, projects, skills, internships, and engineering experiences.
 
27
  Your goal is to represent him truthfully and make his work accessible and understandable to potential collaborators or employers, without overselling or faking.
28
  """
29
 
30
+ BLOCKED_KEYWORDS = ["kill", "harm", "violence", "bomb", "suicide"]
31
  MAX_TOKENS = 512
32
 
33
  # === Streaming generation ===
 
67
  yield partial_message
68
 
69
  # === Gradio interface ===
70
+ with gr.Blocks(title="SBK LLM Chat") as demo:
71
+ gr.Markdown("## Chat with SBK LLM - Professional Portfolio Assistant")
72
+
73
+ with gr.Row():
74
+ with gr.Column(scale=1):
75
+ system_prompt = gr.Textbox(label="System Instructions", value=SYSTEM_PROMPT, lines=8)
76
+ with gr.Column(scale=3):
77
+ chatbot = gr.Chatbot(height=400)
78
+ msg = gr.Textbox(label="Your Message", placeholder="Ask about Saptarshi's professional experience...", lines=2)
79
+ with gr.Row():
80
+ submit_btn = gr.Button("Submit")
81
+ clear_btn = gr.Button("Clear Chat")
82
+
83
+ history = gr.State([])
84
 
85
  def respond(user_message, chat_history, system_prompt):
86
+ chat_history = chat_history + [(user_message, "")]
 
87
 
 
88
  full_response = ""
89
  for response in generate_response(chat_history, system_prompt):
90
  full_response = response
91
  chat_history[-1] = (user_message, full_response)
92
  yield chat_history
93
 
94
+ return chat_history
95
 
96
+ # Connect components
97
  msg.submit(
98
  respond,
99
  [msg, chatbot, system_prompt],
100
+ [chatbot],
101
+ queue=True
102
+ )
103
+ submit_btn.click(
104
+ respond,
105
+ [msg, chatbot, system_prompt],
106
+ [chatbot],
107
+ queue=True
108
+ )
109
+ clear_btn.click(
110
+ lambda: ([], []),
111
+ outputs=[chatbot, history],
112
+ queue=False
113
  )
 
114
 
115
+ # Launch with sharing enabled
116
+ demo.queue(max_size=20).launch(
117
+ share=True,
118
+ server_name="0.0.0.0",
119
+ server_port=7860,
120
+ show_error=True
121
+ )