NexusInstruments commited on
Commit
23bd97d
·
verified ·
1 Parent(s): 03cd1d0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -21
app.py CHANGED
@@ -14,9 +14,26 @@ except ImportError:
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
 
@@ -35,7 +52,6 @@ TASK_MODES = {
35
 
36
  "Behavioral Analysis": """
37
  Conduct a behavioral analysis.
38
-
39
  1. Behavioral Indicators
40
  2. Emotional Tone
41
  3. Cognitive Patterns
@@ -43,7 +59,6 @@ Conduct a behavioral analysis.
43
  5. Risk-Relevant Observations
44
  6. Alternative Explanations
45
  7. Limitations
46
-
47
  Material:
48
  {user_input}
49
  """
@@ -58,11 +73,12 @@ MAX_INPUT_CHARS = 3000
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):
@@ -86,8 +102,19 @@ def extract_text(file):
86
  return ""
87
 
88
 
 
 
 
 
 
 
 
 
 
 
 
89
  # =========================================
90
- # Core Logic (Non-Streaming)
91
  # =========================================
92
 
93
  def generate_response(message, history, model_label, persona_label, task_mode, uploaded_file):
@@ -99,13 +126,12 @@ def generate_response(message, history, model_label, persona_label, task_mode, u
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]
@@ -117,15 +143,20 @@ def generate_response(message, history, model_label, persona_label, task_mode, u
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})
@@ -157,7 +188,7 @@ def export_chat(history):
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
 
@@ -176,7 +207,7 @@ with gr.Blocks(theme=gr.themes.Soft(), title="Omniscient IRIS") as demo:
176
 
177
  model_selector = gr.Dropdown(
178
  choices=list(MODEL_OPTIONS.keys()),
179
- value="Stable (Mistral 7B)",
180
  label="Model"
181
  )
182
 
@@ -226,4 +257,4 @@ with gr.Blocks(theme=gr.themes.Soft(), title="Omniscient IRIS") as demo:
226
 
227
  if __name__ == "__main__":
228
  demo.queue()
229
- demo.launch()
 
14
  # =========================================
15
 
16
  MODEL_OPTIONS = {
17
+ # Serverless-safe models
18
+ "Zephyr 7B (Serverless)": {
19
+ "id": "HuggingFaceH4/zephyr-7b-beta",
20
+ "fallback": False
21
+ },
22
+ "Mixtral 8x7B (Serverless)": {
23
+ "id": "mistralai/Mixtral-8x7B-Instruct-v0.1",
24
+ "fallback": False
25
+ },
26
+
27
+ # Requires Dedicated Endpoint
28
+ "Mistral 7B (Endpoint Required)": {
29
+ "id": "mistralai/Mistral-7B-Instruct-v0.2",
30
+ "fallback": True
31
+ }
32
  }
33
 
34
+ FALLBACK_MODEL_KEY = "Zephyr 7B (Serverless)"
35
+
36
+
37
  PERSONA_PRESETS = {
38
  "Balanced Assistant": "You are a helpful, intelligent AI assistant.",
39
 
 
52
 
53
  "Behavioral Analysis": """
54
  Conduct a behavioral analysis.
 
55
  1. Behavioral Indicators
56
  2. Emotional Tone
57
  3. Cognitive Patterns
 
59
  5. Risk-Relevant Observations
60
  6. Alternative Explanations
61
  7. Limitations
 
62
  Material:
63
  {user_input}
64
  """
 
73
  # Helpers
74
  # =========================================
75
 
76
+ def get_client(model_id):
77
  token = os.environ.get("HF_TOKEN")
78
  if not token:
79
  raise RuntimeError("HF_TOKEN not set in Space Secrets.")
80
+
81
+ return InferenceClient(model=model_id, token=token)
82
 
83
 
84
  def extract_text(file):
 
102
  return ""
103
 
104
 
105
+ def run_model(client, messages, temperature):
106
+ response = client.chat_completion(
107
+ messages=messages,
108
+ max_tokens=700,
109
+ temperature=temperature,
110
+ top_p=0.95,
111
+ stream=False,
112
+ )
113
+ return response.choices[0].message.content
114
+
115
+
116
  # =========================================
117
+ # Core Logic
118
  # =========================================
119
 
120
  def generate_response(message, history, model_label, persona_label, task_mode, uploaded_file):
 
126
  message = message[:MAX_INPUT_CHARS]
127
  history = history[-MAX_HISTORY_PAIRS * 2:]
128
 
129
+ model_config = MODEL_OPTIONS[model_label]
130
+ model_id = model_config["id"]
131
 
132
+ system_prompt = PERSONA_PRESETS[persona_label]
133
  temperature = 0.4 if "Forensic" in persona_label else 0.7
134
 
 
 
135
  file_text = extract_text(uploaded_file)
136
  if file_text:
137
  file_text = file_text[:MAX_CONTEXT_CHARS]
 
143
  formatted_input = TASK_MODES[task_mode].format(user_input=message)
144
  messages.append({"role": "user", "content": formatted_input})
145
 
146
+ # Primary model attempt
147
+ try:
148
+ client = get_client(model_id)
149
+ answer = run_model(client, messages, temperature)
150
+
151
+ # Automatic fallback
152
+ except Exception:
153
+ fallback_model = MODEL_OPTIONS[FALLBACK_MODEL_KEY]["id"]
154
+ fallback_client = get_client(fallback_model)
155
 
156
+ answer = (
157
+ "⚠️ Selected model unavailable. Fallback model used.\n\n"
158
+ + run_model(fallback_client, messages, temperature)
159
+ )
160
 
161
  history.append({"role": "user", "content": message})
162
  history.append({"role": "assistant", "content": answer})
 
188
 
189
  with gr.Blocks(theme=gr.themes.Soft(), title="Omniscient IRIS") as demo:
190
 
191
+ gr.Markdown("## Omniscient IRIS — Adaptive Analysis Assistant")
192
 
193
  with gr.Row():
194
 
 
207
 
208
  model_selector = gr.Dropdown(
209
  choices=list(MODEL_OPTIONS.keys()),
210
+ value=FALLBACK_MODEL_KEY,
211
  label="Model"
212
  )
213
 
 
257
 
258
  if __name__ == "__main__":
259
  demo.queue()
260
+ demo.launch()