NexusInstruments commited on
Commit
e1d4eda
·
verified ·
1 Parent(s): f0ea543

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -48
app.py CHANGED
@@ -19,32 +19,23 @@ MODEL_OPTIONS = {
19
 
20
  PERSONA_PRESETS = {
21
  "Balanced Assistant": "You are a helpful, intelligent AI assistant.",
22
-
23
  "Creative": "You are highly creative and imaginative.",
24
-
25
  "Precise": "You are concise, factual, and analytical.",
26
-
27
- "Code Expert": "You are a senior software engineer who writes clean, well-documented code.",
28
 
29
  "Forensic Psychologist": """
30
  You are acting as a forensic psychologist.
31
-
32
- You provide structured, objective, and evidence-based psychological analysis
33
- within legal and investigative contexts.
34
-
35
- You do NOT diagnose real individuals.
36
- You avoid speculation beyond available evidence.
37
- You clearly separate facts, interpretations, and limitations.
38
  Maintain professional neutrality.
39
-
40
  This analysis is educational and not a clinical diagnosis.
41
  """,
42
 
43
  "Forensic Psychologist (Structured Report)": """
44
- You are acting as a forensic psychologist preparing a structured analytical summary.
45
-
46
- When responding, structure your output using:
47
 
 
48
  1. Case Summary
49
  2. Observed Behaviors
50
  3. Psychological Considerations (Hypothetical)
@@ -53,8 +44,7 @@ When responding, structure your output using:
53
  6. Evidentiary Limitations
54
  7. Professional Disclaimer
55
 
56
- Remain objective and avoid definitive diagnoses.
57
- This analysis is educational and not a clinical diagnosis.
58
  """
59
  }
60
 
@@ -65,9 +55,9 @@ TASK_MODES = {
65
  Conduct a behavioral analysis.
66
 
67
  1. Behavioral Indicators
68
- 2. Emotional Tone & Affect
69
  3. Cognitive Patterns
70
- 4. Stress or Deception Markers
71
  5. Risk-Relevant Observations
72
  6. Alternative Explanations
73
  7. Limitations
@@ -108,7 +98,7 @@ Material:
108
  }
109
 
110
  MAX_CONTEXT_CHARS = 12000
111
- MAX_HISTORY = 8 # memory cap for stability
112
 
113
  # =========================================
114
  # File Processing
@@ -144,49 +134,43 @@ def stream_response(message, history, model_label, persona_label, task_mode, upl
144
  yield history
145
  return
146
 
147
- # Guard message length
148
  message = message[:4000]
149
 
150
- # Limit memory
151
- history = history[-MAX_HISTORY:]
152
 
153
  model_name = MODEL_OPTIONS[model_label]
154
  base_system_prompt = PERSONA_PRESETS[persona_label]
155
 
156
- # Temperature adjustment
157
- if "Forensic Psychologist" in persona_label:
158
- temperature = 0.4
159
- else:
160
- temperature = 0.7
161
 
162
  hf_token = os.environ.get("HF_TOKEN")
163
  client = InferenceClient(model=model_name, token=hf_token)
164
 
165
  # File context
166
  file_text = extract_text(uploaded_file)
167
-
168
  if file_text:
169
  file_text = file_text[:MAX_CONTEXT_CHARS]
170
  system_prompt = (
171
  f"{base_system_prompt}\n\n"
172
- f"Use the following document as context when relevant:\n\n"
173
- f"{file_text}"
174
  )
175
  else:
176
  system_prompt = base_system_prompt
177
 
 
178
  messages = [{"role": "system", "content": system_prompt}]
 
179
 
180
- for user_msg, assistant_msg in history:
181
- messages.append({"role": "user", "content": user_msg})
182
- messages.append({"role": "assistant", "content": assistant_msg})
183
 
184
- formatted_message = TASK_MODES[task_mode].format(user_input=message)
185
- messages.append({"role": "user", "content": formatted_message})
 
186
 
187
- history.append((message, ""))
188
-
189
- partial_response = ""
190
 
191
  for chunk in client.chat_completion(
192
  messages=messages,
@@ -197,8 +181,8 @@ def stream_response(message, history, model_label, persona_label, task_mode, upl
197
  ):
198
  if chunk.choices and chunk.choices[0].delta.content:
199
  token = chunk.choices[0].delta.content
200
- partial_response += token
201
- history[-1] = (message, partial_response)
202
  yield history
203
 
204
  # =========================================
@@ -209,10 +193,7 @@ def clear_chat():
209
  return []
210
 
211
  def export_chat(history):
212
- return json.dumps(
213
- [{"user": u, "assistant": a} for u, a in history],
214
- indent=2
215
- )
216
 
217
  # =========================================
218
  # UI
@@ -223,16 +204,17 @@ with gr.Blocks(theme=gr.themes.Soft(), title="Omniscient IRIS") as demo:
223
  gr.Markdown("## Omniscient IRIS — Intelligent Analysis Assistant")
224
 
225
  with gr.Row():
 
226
  with gr.Column(scale=3):
227
 
228
- chatbot = gr.Chatbot(height=500)
229
  msg = gr.Textbox(
230
  placeholder="Enter material for analysis or ask a question...",
231
  show_label=False
232
  )
233
 
234
  with gr.Row():
235
- submit_btn = gr.Button("Send", variant="primary")
236
  stop_btn = gr.Button("Stop")
237
  clear_btn = gr.Button("Clear")
238
 
@@ -265,7 +247,7 @@ with gr.Blocks(theme=gr.themes.Soft(), title="Omniscient IRIS") as demo:
265
 
266
  state = gr.State([])
267
 
268
- submit_event = submit_btn.click(
269
  stream_response,
270
  inputs=[msg, state, model_selector, persona_selector, task_selector, file_upload],
271
  outputs=chatbot,
 
19
 
20
  PERSONA_PRESETS = {
21
  "Balanced Assistant": "You are a helpful, intelligent AI assistant.",
 
22
  "Creative": "You are highly creative and imaginative.",
 
23
  "Precise": "You are concise, factual, and analytical.",
24
+ "Code Expert": "You are a senior software engineer who writes clean code.",
 
25
 
26
  "Forensic Psychologist": """
27
  You are acting as a forensic psychologist.
28
+ Provide structured, objective, evidence-based analysis.
29
+ Do NOT diagnose real individuals.
30
+ Separate facts, interpretations, and limitations.
 
 
 
 
31
  Maintain professional neutrality.
 
32
  This analysis is educational and not a clinical diagnosis.
33
  """,
34
 
35
  "Forensic Psychologist (Structured Report)": """
36
+ You are acting as a forensic psychologist preparing a structured report.
 
 
37
 
38
+ Structure:
39
  1. Case Summary
40
  2. Observed Behaviors
41
  3. Psychological Considerations (Hypothetical)
 
44
  6. Evidentiary Limitations
45
  7. Professional Disclaimer
46
 
47
+ Remain objective and non-diagnostic.
 
48
  """
49
  }
50
 
 
55
  Conduct a behavioral analysis.
56
 
57
  1. Behavioral Indicators
58
+ 2. Emotional Tone
59
  3. Cognitive Patterns
60
+ 4. Stress/Deception Markers
61
  5. Risk-Relevant Observations
62
  6. Alternative Explanations
63
  7. Limitations
 
98
  }
99
 
100
  MAX_CONTEXT_CHARS = 12000
101
+ MAX_HISTORY_PAIRS = 8 # 8 exchanges
102
 
103
  # =========================================
104
  # File Processing
 
134
  yield history
135
  return
136
 
137
+ # Cap input length
138
  message = message[:4000]
139
 
140
+ # Cap memory (each pair = 2 messages)
141
+ history = history[-MAX_HISTORY_PAIRS * 2:]
142
 
143
  model_name = MODEL_OPTIONS[model_label]
144
  base_system_prompt = PERSONA_PRESETS[persona_label]
145
 
146
+ temperature = 0.4 if "Forensic Psychologist" in persona_label else 0.7
 
 
 
 
147
 
148
  hf_token = os.environ.get("HF_TOKEN")
149
  client = InferenceClient(model=model_name, token=hf_token)
150
 
151
  # File context
152
  file_text = extract_text(uploaded_file)
 
153
  if file_text:
154
  file_text = file_text[:MAX_CONTEXT_CHARS]
155
  system_prompt = (
156
  f"{base_system_prompt}\n\n"
157
+ f"Use this document context when relevant:\n\n{file_text}"
 
158
  )
159
  else:
160
  system_prompt = base_system_prompt
161
 
162
+ # Build messages
163
  messages = [{"role": "system", "content": system_prompt}]
164
+ messages.extend(history)
165
 
166
+ formatted_input = TASK_MODES[task_mode].format(user_input=message)
167
+ messages.append({"role": "user", "content": formatted_input})
 
168
 
169
+ # Update UI history (Gradio 5 format)
170
+ history.append({"role": "user", "content": message})
171
+ history.append({"role": "assistant", "content": ""})
172
 
173
+ partial = ""
 
 
174
 
175
  for chunk in client.chat_completion(
176
  messages=messages,
 
181
  ):
182
  if chunk.choices and chunk.choices[0].delta.content:
183
  token = chunk.choices[0].delta.content
184
+ partial += token
185
+ history[-1]["content"] = partial
186
  yield history
187
 
188
  # =========================================
 
193
  return []
194
 
195
  def export_chat(history):
196
+ return json.dumps(history, indent=2)
 
 
 
197
 
198
  # =========================================
199
  # UI
 
204
  gr.Markdown("## Omniscient IRIS — Intelligent Analysis Assistant")
205
 
206
  with gr.Row():
207
+
208
  with gr.Column(scale=3):
209
 
210
+ chatbot = gr.Chatbot(height=500, type="messages")
211
  msg = gr.Textbox(
212
  placeholder="Enter material for analysis or ask a question...",
213
  show_label=False
214
  )
215
 
216
  with gr.Row():
217
+ send_btn = gr.Button("Send", variant="primary")
218
  stop_btn = gr.Button("Stop")
219
  clear_btn = gr.Button("Clear")
220
 
 
247
 
248
  state = gr.State([])
249
 
250
+ submit_event = send_btn.click(
251
  stream_response,
252
  inputs=[msg, state, model_selector, persona_selector, task_selector, file_upload],
253
  outputs=chatbot,