Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -19,13 +19,96 @@ MODEL_OPTIONS = {
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
}
|
| 26 |
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
|
|
|
|
|
|
| 29 |
|
| 30 |
# =========================================
|
| 31 |
# File Processing
|
|
@@ -37,7 +120,7 @@ def extract_text(file):
|
|
| 37 |
|
| 38 |
file_path = file.name
|
| 39 |
|
| 40 |
-
if file_path.endswith(".txt"
|
| 41 |
with open(file_path, "r", encoding="utf-8", errors="ignore") as f:
|
| 42 |
return f.read()
|
| 43 |
|
|
@@ -51,24 +134,35 @@ def extract_text(file):
|
|
| 51 |
|
| 52 |
return ""
|
| 53 |
|
| 54 |
-
|
| 55 |
# =========================================
|
| 56 |
# Core Chat Logic
|
| 57 |
# =========================================
|
| 58 |
|
| 59 |
-
def stream_response(message, history, model_label, persona_label, uploaded_file):
|
| 60 |
|
| 61 |
if not message:
|
| 62 |
yield history
|
| 63 |
return
|
| 64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
model_name = MODEL_OPTIONS[model_label]
|
| 66 |
base_system_prompt = PERSONA_PRESETS[persona_label]
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
hf_token = os.environ.get("HF_TOKEN")
|
| 69 |
client = InferenceClient(model=model_name, token=hf_token)
|
| 70 |
|
| 71 |
-
#
|
| 72 |
file_text = extract_text(uploaded_file)
|
| 73 |
|
| 74 |
if file_text:
|
|
@@ -87,7 +181,8 @@ def stream_response(message, history, model_label, persona_label, uploaded_file)
|
|
| 87 |
messages.append({"role": "user", "content": user_msg})
|
| 88 |
messages.append({"role": "assistant", "content": assistant_msg})
|
| 89 |
|
| 90 |
-
|
|
|
|
| 91 |
|
| 92 |
history.append((message, ""))
|
| 93 |
|
|
@@ -96,7 +191,7 @@ def stream_response(message, history, model_label, persona_label, uploaded_file)
|
|
| 96 |
for chunk in client.chat_completion(
|
| 97 |
messages=messages,
|
| 98 |
max_tokens=800,
|
| 99 |
-
temperature=
|
| 100 |
top_p=0.95,
|
| 101 |
stream=True,
|
| 102 |
):
|
|
@@ -106,7 +201,6 @@ def stream_response(message, history, model_label, persona_label, uploaded_file)
|
|
| 106 |
history[-1] = (message, partial_response)
|
| 107 |
yield history
|
| 108 |
|
| 109 |
-
|
| 110 |
# =========================================
|
| 111 |
# Utilities
|
| 112 |
# =========================================
|
|
@@ -120,21 +214,20 @@ def export_chat(history):
|
|
| 120 |
indent=2
|
| 121 |
)
|
| 122 |
|
| 123 |
-
|
| 124 |
# =========================================
|
| 125 |
# UI
|
| 126 |
# =========================================
|
| 127 |
|
| 128 |
with gr.Blocks(theme=gr.themes.Soft(), title="Omniscient IRIS") as demo:
|
| 129 |
|
| 130 |
-
gr.Markdown("## Omniscient IRIS —
|
| 131 |
|
| 132 |
with gr.Row():
|
| 133 |
with gr.Column(scale=3):
|
| 134 |
|
| 135 |
chatbot = gr.Chatbot(height=500)
|
| 136 |
msg = gr.Textbox(
|
| 137 |
-
placeholder="
|
| 138 |
show_label=False
|
| 139 |
)
|
| 140 |
|
|
@@ -157,6 +250,12 @@ with gr.Blocks(theme=gr.themes.Soft(), title="Omniscient IRIS") as demo:
|
|
| 157 |
label="Persona"
|
| 158 |
)
|
| 159 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
file_upload = gr.File(
|
| 161 |
label="Upload Document (txt, pdf, md)",
|
| 162 |
file_types=[".txt", ".pdf", ".md"]
|
|
@@ -168,13 +267,13 @@ with gr.Blocks(theme=gr.themes.Soft(), title="Omniscient IRIS") as demo:
|
|
| 168 |
|
| 169 |
submit_event = submit_btn.click(
|
| 170 |
stream_response,
|
| 171 |
-
inputs=[msg, state, model_selector, persona_selector, file_upload],
|
| 172 |
outputs=chatbot,
|
| 173 |
)
|
| 174 |
|
| 175 |
msg.submit(
|
| 176 |
stream_response,
|
| 177 |
-
inputs=[msg, state, model_selector, persona_selector, file_upload],
|
| 178 |
outputs=chatbot,
|
| 179 |
)
|
| 180 |
|
|
@@ -194,7 +293,6 @@ with gr.Blocks(theme=gr.themes.Soft(), title="Omniscient IRIS") as demo:
|
|
| 194 |
outputs=gr.File(label="Download JSON")
|
| 195 |
)
|
| 196 |
|
| 197 |
-
|
| 198 |
if __name__ == "__main__":
|
| 199 |
demo.queue()
|
| 200 |
demo.launch()
|
|
|
|
| 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)
|
| 51 |
+
4. Risk Assessment Factors
|
| 52 |
+
5. Alternative Explanations
|
| 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 |
|
| 61 |
+
TASK_MODES = {
|
| 62 |
+
"General Chat": "{user_input}",
|
| 63 |
+
|
| 64 |
+
"Behavioral Analysis": """
|
| 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
|
| 74 |
+
|
| 75 |
+
Material:
|
| 76 |
+
{user_input}
|
| 77 |
+
""",
|
| 78 |
+
|
| 79 |
+
"Linguistic Analysis": """
|
| 80 |
+
Conduct a linguistic analysis.
|
| 81 |
+
|
| 82 |
+
1. Language Patterns
|
| 83 |
+
2. Pronoun Usage
|
| 84 |
+
3. Emotional Indicators
|
| 85 |
+
4. Cognitive Load Markers
|
| 86 |
+
5. Narrative Shifts
|
| 87 |
+
6. Possible Deception Signals
|
| 88 |
+
7. Limitations
|
| 89 |
+
|
| 90 |
+
Material:
|
| 91 |
+
{user_input}
|
| 92 |
+
""",
|
| 93 |
+
|
| 94 |
+
"Credibility Analysis": """
|
| 95 |
+
Conduct a credibility analysis.
|
| 96 |
+
|
| 97 |
+
1. Internal Consistency
|
| 98 |
+
2. Plausibility
|
| 99 |
+
3. Specificity vs Vagueness
|
| 100 |
+
4. Emotional Congruence
|
| 101 |
+
5. Possible Fabrication Indicators
|
| 102 |
+
6. Alternative Explanations
|
| 103 |
+
7. Evidentiary Limitations
|
| 104 |
+
|
| 105 |
+
Material:
|
| 106 |
+
{user_input}
|
| 107 |
+
"""
|
| 108 |
+
}
|
| 109 |
|
| 110 |
+
MAX_CONTEXT_CHARS = 12000
|
| 111 |
+
MAX_HISTORY = 8 # memory cap for stability
|
| 112 |
|
| 113 |
# =========================================
|
| 114 |
# File Processing
|
|
|
|
| 120 |
|
| 121 |
file_path = file.name
|
| 122 |
|
| 123 |
+
if file_path.endswith((".txt", ".md")):
|
| 124 |
with open(file_path, "r", encoding="utf-8", errors="ignore") as f:
|
| 125 |
return f.read()
|
| 126 |
|
|
|
|
| 134 |
|
| 135 |
return ""
|
| 136 |
|
|
|
|
| 137 |
# =========================================
|
| 138 |
# Core Chat Logic
|
| 139 |
# =========================================
|
| 140 |
|
| 141 |
+
def stream_response(message, history, model_label, persona_label, task_mode, uploaded_file):
|
| 142 |
|
| 143 |
if not message:
|
| 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:
|
|
|
|
| 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 |
|
|
|
|
| 191 |
for chunk in client.chat_completion(
|
| 192 |
messages=messages,
|
| 193 |
max_tokens=800,
|
| 194 |
+
temperature=temperature,
|
| 195 |
top_p=0.95,
|
| 196 |
stream=True,
|
| 197 |
):
|
|
|
|
| 201 |
history[-1] = (message, partial_response)
|
| 202 |
yield history
|
| 203 |
|
|
|
|
| 204 |
# =========================================
|
| 205 |
# Utilities
|
| 206 |
# =========================================
|
|
|
|
| 214 |
indent=2
|
| 215 |
)
|
| 216 |
|
|
|
|
| 217 |
# =========================================
|
| 218 |
# UI
|
| 219 |
# =========================================
|
| 220 |
|
| 221 |
with gr.Blocks(theme=gr.themes.Soft(), title="Omniscient IRIS") as demo:
|
| 222 |
|
| 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 |
|
|
|
|
| 250 |
label="Persona"
|
| 251 |
)
|
| 252 |
|
| 253 |
+
task_selector = gr.Dropdown(
|
| 254 |
+
choices=list(TASK_MODES.keys()),
|
| 255 |
+
value="General Chat",
|
| 256 |
+
label="Analysis Mode"
|
| 257 |
+
)
|
| 258 |
+
|
| 259 |
file_upload = gr.File(
|
| 260 |
label="Upload Document (txt, pdf, md)",
|
| 261 |
file_types=[".txt", ".pdf", ".md"]
|
|
|
|
| 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,
|
| 272 |
)
|
| 273 |
|
| 274 |
msg.submit(
|
| 275 |
stream_response,
|
| 276 |
+
inputs=[msg, state, model_selector, persona_selector, task_selector, file_upload],
|
| 277 |
outputs=chatbot,
|
| 278 |
)
|
| 279 |
|
|
|
|
| 293 |
outputs=gr.File(label="Download JSON")
|
| 294 |
)
|
| 295 |
|
|
|
|
| 296 |
if __name__ == "__main__":
|
| 297 |
demo.queue()
|
| 298 |
demo.launch()
|