Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,15 +14,11 @@ except ImportError:
|
|
| 14 |
# =========================================
|
| 15 |
|
| 16 |
MODEL_OPTIONS = {
|
| 17 |
-
"
|
| 18 |
-
"Balanced (GPT-OSS 20B)": "openai/gpt-oss-20b",
|
| 19 |
}
|
| 20 |
|
| 21 |
PERSONA_PRESETS = {
|
| 22 |
"Balanced Assistant": "You are a helpful, intelligent AI assistant.",
|
| 23 |
-
"Creative": "You are highly creative and imaginative.",
|
| 24 |
-
"Precise": "You are concise, factual, and analytical.",
|
| 25 |
-
"Code Expert": "You are a senior software engineer who writes clean code.",
|
| 26 |
|
| 27 |
"Forensic Psychologist": """
|
| 28 |
You are acting as a forensic psychologist.
|
|
@@ -31,21 +27,6 @@ Do NOT diagnose real individuals.
|
|
| 31 |
Separate facts, interpretations, and limitations.
|
| 32 |
Maintain professional neutrality.
|
| 33 |
This analysis is educational and not a clinical diagnosis.
|
| 34 |
-
""",
|
| 35 |
-
|
| 36 |
-
"Forensic Psychologist (Structured Report)": """
|
| 37 |
-
You are acting as a forensic psychologist preparing a structured report.
|
| 38 |
-
|
| 39 |
-
Structure:
|
| 40 |
-
1. Case Summary
|
| 41 |
-
2. Observed Behaviors
|
| 42 |
-
3. Psychological Considerations (Hypothetical)
|
| 43 |
-
4. Risk Assessment Factors
|
| 44 |
-
5. Alternative Explanations
|
| 45 |
-
6. Evidentiary Limitations
|
| 46 |
-
7. Professional Disclaimer
|
| 47 |
-
|
| 48 |
-
Remain objective and non-diagnostic.
|
| 49 |
"""
|
| 50 |
}
|
| 51 |
|
|
@@ -63,55 +44,25 @@ Conduct a behavioral analysis.
|
|
| 63 |
6. Alternative Explanations
|
| 64 |
7. Limitations
|
| 65 |
|
| 66 |
-
Material:
|
| 67 |
-
{user_input}
|
| 68 |
-
""",
|
| 69 |
-
|
| 70 |
-
"Linguistic Analysis": """
|
| 71 |
-
Conduct a linguistic analysis.
|
| 72 |
-
|
| 73 |
-
1. Language Patterns
|
| 74 |
-
2. Pronoun Usage
|
| 75 |
-
3. Emotional Indicators
|
| 76 |
-
4. Cognitive Load Markers
|
| 77 |
-
5. Narrative Shifts
|
| 78 |
-
6. Possible Deception Signals
|
| 79 |
-
7. Limitations
|
| 80 |
-
|
| 81 |
-
Material:
|
| 82 |
-
{user_input}
|
| 83 |
-
""",
|
| 84 |
-
|
| 85 |
-
"Credibility Analysis": """
|
| 86 |
-
Conduct a credibility analysis.
|
| 87 |
-
|
| 88 |
-
1. Internal Consistency
|
| 89 |
-
2. Plausibility
|
| 90 |
-
3. Specificity vs Vagueness
|
| 91 |
-
4. Emotional Congruence
|
| 92 |
-
5. Possible Fabrication Indicators
|
| 93 |
-
6. Alternative Explanations
|
| 94 |
-
7. Evidentiary Limitations
|
| 95 |
-
|
| 96 |
Material:
|
| 97 |
{user_input}
|
| 98 |
"""
|
| 99 |
}
|
| 100 |
|
| 101 |
-
MAX_CONTEXT_CHARS =
|
| 102 |
-
MAX_HISTORY_PAIRS =
|
| 103 |
-
MAX_INPUT_CHARS =
|
| 104 |
|
| 105 |
|
| 106 |
# =========================================
|
| 107 |
# Helpers
|
| 108 |
# =========================================
|
| 109 |
|
| 110 |
-
def get_client(model_name
|
| 111 |
-
|
| 112 |
-
if not
|
| 113 |
-
raise RuntimeError("HF_TOKEN not set
|
| 114 |
-
return InferenceClient(model=model_name, token=
|
| 115 |
|
| 116 |
|
| 117 |
def extract_text(file):
|
|
@@ -136,54 +87,54 @@ def extract_text(file):
|
|
| 136 |
|
| 137 |
|
| 138 |
# =========================================
|
| 139 |
-
# Core
|
| 140 |
# =========================================
|
| 141 |
|
| 142 |
-
def
|
| 143 |
|
| 144 |
if not message:
|
| 145 |
-
|
| 146 |
-
|
|
|
|
|
|
|
|
|
|
| 147 |
|
| 148 |
-
|
| 149 |
-
|
| 150 |
|
| 151 |
-
|
| 152 |
-
system_prompt = PERSONA_PRESETS[persona_label]
|
| 153 |
|
| 154 |
-
|
| 155 |
|
| 156 |
-
|
|
|
|
|
|
|
|
|
|
| 157 |
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
file_text = file_text[:MAX_CONTEXT_CHARS]
|
| 161 |
-
system_prompt += f"\n\nUse this document context when relevant:\n\n{file_text}"
|
| 162 |
|
| 163 |
-
|
| 164 |
-
|
| 165 |
|
| 166 |
-
|
| 167 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
|
| 169 |
-
|
| 170 |
-
history.append({"role": "assistant", "content": ""})
|
| 171 |
|
| 172 |
-
|
|
|
|
| 173 |
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
stream=True,
|
| 180 |
-
):
|
| 181 |
-
if chunk.choices:
|
| 182 |
-
delta = chunk.choices[0].delta
|
| 183 |
-
if hasattr(delta, "content") and delta.content:
|
| 184 |
-
partial += delta.content
|
| 185 |
-
history[-1]["content"] = partial
|
| 186 |
-
yield history
|
| 187 |
|
| 188 |
|
| 189 |
# =========================================
|
|
@@ -206,7 +157,7 @@ def export_chat(history):
|
|
| 206 |
|
| 207 |
with gr.Blocks(theme=gr.themes.Soft(), title="Omniscient IRIS") as demo:
|
| 208 |
|
| 209 |
-
gr.Markdown("## Omniscient IRIS —
|
| 210 |
|
| 211 |
with gr.Row():
|
| 212 |
|
|
@@ -218,16 +169,14 @@ with gr.Blocks(theme=gr.themes.Soft(), title="Omniscient IRIS") as demo:
|
|
| 218 |
show_label=False
|
| 219 |
)
|
| 220 |
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
stop_btn = gr.Button("Stop")
|
| 224 |
-
clear_btn = gr.Button("Clear")
|
| 225 |
|
| 226 |
with gr.Column(scale=1):
|
| 227 |
|
| 228 |
model_selector = gr.Dropdown(
|
| 229 |
choices=list(MODEL_OPTIONS.keys()),
|
| 230 |
-
value="
|
| 231 |
label="Model"
|
| 232 |
)
|
| 233 |
|
|
@@ -252,20 +201,18 @@ with gr.Blocks(theme=gr.themes.Soft(), title="Omniscient IRIS") as demo:
|
|
| 252 |
|
| 253 |
state = gr.State([])
|
| 254 |
|
| 255 |
-
|
| 256 |
-
|
| 257 |
inputs=[msg, state, model_selector, persona_selector, task_selector, file_upload],
|
| 258 |
outputs=chatbot,
|
| 259 |
)
|
| 260 |
|
| 261 |
msg.submit(
|
| 262 |
-
|
| 263 |
inputs=[msg, state, model_selector, persona_selector, task_selector, file_upload],
|
| 264 |
outputs=chatbot,
|
| 265 |
)
|
| 266 |
|
| 267 |
-
stop_btn.click(fn=None, cancels=[submit_event])
|
| 268 |
-
|
| 269 |
clear_btn.click(
|
| 270 |
fn=clear_chat,
|
| 271 |
outputs=chatbot,
|
|
|
|
| 14 |
# =========================================
|
| 15 |
|
| 16 |
MODEL_OPTIONS = {
|
| 17 |
+
"Stable (Mistral 7B)": "mistralai/Mistral-7B-Instruct-v0.2",
|
|
|
|
| 18 |
}
|
| 19 |
|
| 20 |
PERSONA_PRESETS = {
|
| 21 |
"Balanced Assistant": "You are a helpful, intelligent AI assistant.",
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
"Forensic Psychologist": """
|
| 24 |
You are acting as a forensic psychologist.
|
|
|
|
| 27 |
Separate facts, interpretations, and limitations.
|
| 28 |
Maintain professional neutrality.
|
| 29 |
This analysis is educational and not a clinical diagnosis.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
"""
|
| 31 |
}
|
| 32 |
|
|
|
|
| 44 |
6. Alternative Explanations
|
| 45 |
7. Limitations
|
| 46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
Material:
|
| 48 |
{user_input}
|
| 49 |
"""
|
| 50 |
}
|
| 51 |
|
| 52 |
+
MAX_CONTEXT_CHARS = 10000
|
| 53 |
+
MAX_HISTORY_PAIRS = 6
|
| 54 |
+
MAX_INPUT_CHARS = 3000
|
| 55 |
|
| 56 |
|
| 57 |
# =========================================
|
| 58 |
# Helpers
|
| 59 |
# =========================================
|
| 60 |
|
| 61 |
+
def get_client(model_name):
|
| 62 |
+
token = os.environ.get("HF_TOKEN")
|
| 63 |
+
if not token:
|
| 64 |
+
raise RuntimeError("HF_TOKEN not set in Space Secrets.")
|
| 65 |
+
return InferenceClient(model=model_name, token=token)
|
| 66 |
|
| 67 |
|
| 68 |
def extract_text(file):
|
|
|
|
| 87 |
|
| 88 |
|
| 89 |
# =========================================
|
| 90 |
+
# Core Logic (Non-Streaming)
|
| 91 |
# =========================================
|
| 92 |
|
| 93 |
+
def generate_response(message, history, model_label, persona_label, task_mode, uploaded_file):
|
| 94 |
|
| 95 |
if not message:
|
| 96 |
+
return history
|
| 97 |
+
|
| 98 |
+
try:
|
| 99 |
+
message = message[:MAX_INPUT_CHARS]
|
| 100 |
+
history = history[-MAX_HISTORY_PAIRS * 2:]
|
| 101 |
|
| 102 |
+
model_name = MODEL_OPTIONS[model_label]
|
| 103 |
+
system_prompt = PERSONA_PRESETS[persona_label]
|
| 104 |
|
| 105 |
+
temperature = 0.4 if "Forensic" in persona_label else 0.7
|
|
|
|
| 106 |
|
| 107 |
+
client = get_client(model_name)
|
| 108 |
|
| 109 |
+
file_text = extract_text(uploaded_file)
|
| 110 |
+
if file_text:
|
| 111 |
+
file_text = file_text[:MAX_CONTEXT_CHARS]
|
| 112 |
+
system_prompt += f"\n\nUse this document context when relevant:\n\n{file_text}"
|
| 113 |
|
| 114 |
+
messages = [{"role": "system", "content": system_prompt}]
|
| 115 |
+
messages.extend(history)
|
|
|
|
|
|
|
| 116 |
|
| 117 |
+
formatted_input = TASK_MODES[task_mode].format(user_input=message)
|
| 118 |
+
messages.append({"role": "user", "content": formatted_input})
|
| 119 |
|
| 120 |
+
response = client.chat_completion(
|
| 121 |
+
messages=messages,
|
| 122 |
+
max_tokens=700,
|
| 123 |
+
temperature=temperature,
|
| 124 |
+
top_p=0.95,
|
| 125 |
+
stream=False,
|
| 126 |
+
)
|
| 127 |
|
| 128 |
+
answer = response.choices[0].message.content
|
|
|
|
| 129 |
|
| 130 |
+
history.append({"role": "user", "content": message})
|
| 131 |
+
history.append({"role": "assistant", "content": answer})
|
| 132 |
|
| 133 |
+
return history
|
| 134 |
+
|
| 135 |
+
except Exception as e:
|
| 136 |
+
history.append({"role": "assistant", "content": f"⚠️ ERROR:\n\n{str(e)}"})
|
| 137 |
+
return history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
|
| 139 |
|
| 140 |
# =========================================
|
|
|
|
| 157 |
|
| 158 |
with gr.Blocks(theme=gr.themes.Soft(), title="Omniscient IRIS") as demo:
|
| 159 |
|
| 160 |
+
gr.Markdown("## Omniscient IRIS — Stable Analysis Assistant")
|
| 161 |
|
| 162 |
with gr.Row():
|
| 163 |
|
|
|
|
| 169 |
show_label=False
|
| 170 |
)
|
| 171 |
|
| 172 |
+
send_btn = gr.Button("Send", variant="primary")
|
| 173 |
+
clear_btn = gr.Button("Clear")
|
|
|
|
|
|
|
| 174 |
|
| 175 |
with gr.Column(scale=1):
|
| 176 |
|
| 177 |
model_selector = gr.Dropdown(
|
| 178 |
choices=list(MODEL_OPTIONS.keys()),
|
| 179 |
+
value="Stable (Mistral 7B)",
|
| 180 |
label="Model"
|
| 181 |
)
|
| 182 |
|
|
|
|
| 201 |
|
| 202 |
state = gr.State([])
|
| 203 |
|
| 204 |
+
send_btn.click(
|
| 205 |
+
generate_response,
|
| 206 |
inputs=[msg, state, model_selector, persona_selector, task_selector, file_upload],
|
| 207 |
outputs=chatbot,
|
| 208 |
)
|
| 209 |
|
| 210 |
msg.submit(
|
| 211 |
+
generate_response,
|
| 212 |
inputs=[msg, state, model_selector, persona_selector, task_selector, file_upload],
|
| 213 |
outputs=chatbot,
|
| 214 |
)
|
| 215 |
|
|
|
|
|
|
|
| 216 |
clear_btn.click(
|
| 217 |
fn=clear_chat,
|
| 218 |
outputs=chatbot,
|