Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,9 @@
|
|
| 1 |
import os
|
| 2 |
import json
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
from huggingface_hub import InferenceClient
|
|
|
|
| 5 |
|
| 6 |
try:
|
| 7 |
import PyPDF2
|
|
@@ -14,25 +16,11 @@ except ImportError:
|
|
| 14 |
# =========================================
|
| 15 |
|
| 16 |
MODEL_OPTIONS = {
|
| 17 |
-
|
| 18 |
-
"
|
| 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 |
-
|
| 35 |
-
|
| 36 |
|
| 37 |
PERSONA_PRESETS = {
|
| 38 |
"Balanced Assistant": "You are a helpful, intelligent AI assistant.",
|
|
@@ -61,6 +49,14 @@ Conduct a behavioral analysis.
|
|
| 61 |
7. Limitations
|
| 62 |
Material:
|
| 63 |
{user_input}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
"""
|
| 65 |
}
|
| 66 |
|
|
@@ -77,7 +73,6 @@ 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 |
|
|
@@ -102,6 +97,13 @@ def extract_text(file):
|
|
| 102 |
return ""
|
| 103 |
|
| 104 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
def run_model(client, messages, temperature):
|
| 106 |
response = client.chat_completion(
|
| 107 |
messages=messages,
|
|
@@ -117,19 +119,17 @@ def run_model(client, messages, temperature):
|
|
| 117 |
# Core Logic
|
| 118 |
# =========================================
|
| 119 |
|
| 120 |
-
def generate_response(message, history, model_label, persona_label, task_mode, uploaded_file):
|
| 121 |
|
| 122 |
if not message:
|
| 123 |
-
return history
|
| 124 |
|
| 125 |
try:
|
| 126 |
message = message[:MAX_INPUT_CHARS]
|
| 127 |
history = history[-MAX_HISTORY_PAIRS * 2:]
|
| 128 |
|
| 129 |
-
|
| 130 |
-
|
| 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)
|
|
@@ -143,29 +143,25 @@ def generate_response(message, history, model_label, persona_label, task_mode, u
|
|
| 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 |
-
|
| 154 |
-
|
| 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})
|
| 163 |
|
| 164 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
|
| 166 |
except Exception as e:
|
| 167 |
history.append({"role": "assistant", "content": f"⚠️ ERROR:\n\n{str(e)}"})
|
| 168 |
-
return history
|
| 169 |
|
| 170 |
|
| 171 |
# =========================================
|
|
@@ -173,7 +169,7 @@ def generate_response(message, history, model_label, persona_label, task_mode, u
|
|
| 173 |
# =========================================
|
| 174 |
|
| 175 |
def clear_chat():
|
| 176 |
-
return []
|
| 177 |
|
| 178 |
def export_chat(history):
|
| 179 |
file_path = "conversation.json"
|
|
@@ -188,7 +184,7 @@ def export_chat(history):
|
|
| 188 |
|
| 189 |
with gr.Blocks(theme=gr.themes.Soft(), title="Omniscient IRIS") as demo:
|
| 190 |
|
| 191 |
-
gr.Markdown("## Omniscient IRIS —
|
| 192 |
|
| 193 |
with gr.Row():
|
| 194 |
|
|
@@ -200,6 +196,8 @@ with gr.Blocks(theme=gr.themes.Soft(), title="Omniscient IRIS") as demo:
|
|
| 200 |
show_label=False
|
| 201 |
)
|
| 202 |
|
|
|
|
|
|
|
| 203 |
send_btn = gr.Button("Send", variant="primary")
|
| 204 |
clear_btn = gr.Button("Clear")
|
| 205 |
|
|
@@ -207,7 +205,7 @@ with gr.Blocks(theme=gr.themes.Soft(), title="Omniscient IRIS") as demo:
|
|
| 207 |
|
| 208 |
model_selector = gr.Dropdown(
|
| 209 |
choices=list(MODEL_OPTIONS.keys()),
|
| 210 |
-
value=
|
| 211 |
label="Model"
|
| 212 |
)
|
| 213 |
|
|
@@ -220,9 +218,11 @@ with gr.Blocks(theme=gr.themes.Soft(), title="Omniscient IRIS") as demo:
|
|
| 220 |
task_selector = gr.Dropdown(
|
| 221 |
choices=list(TASK_MODES.keys()),
|
| 222 |
value="General Chat",
|
| 223 |
-
label="
|
| 224 |
)
|
| 225 |
|
|
|
|
|
|
|
| 226 |
file_upload = gr.File(
|
| 227 |
label="Upload Document (txt, pdf, md)",
|
| 228 |
file_types=[".txt", ".pdf", ".md"]
|
|
@@ -234,19 +234,19 @@ with gr.Blocks(theme=gr.themes.Soft(), title="Omniscient IRIS") as demo:
|
|
| 234 |
|
| 235 |
send_btn.click(
|
| 236 |
generate_response,
|
| 237 |
-
inputs=[msg, state, model_selector, persona_selector, task_selector, file_upload],
|
| 238 |
-
outputs=chatbot,
|
| 239 |
)
|
| 240 |
|
| 241 |
msg.submit(
|
| 242 |
generate_response,
|
| 243 |
-
inputs=[msg, state, model_selector, persona_selector, task_selector, file_upload],
|
| 244 |
-
outputs=chatbot,
|
| 245 |
)
|
| 246 |
|
| 247 |
clear_btn.click(
|
| 248 |
fn=clear_chat,
|
| 249 |
-
outputs=chatbot,
|
| 250 |
).then(lambda: [], outputs=state)
|
| 251 |
|
| 252 |
export_btn.click(
|
|
|
|
| 1 |
import os
|
| 2 |
import json
|
| 3 |
+
import tempfile
|
| 4 |
import gradio as gr
|
| 5 |
from huggingface_hub import InferenceClient
|
| 6 |
+
from gtts import gTTS
|
| 7 |
|
| 8 |
try:
|
| 9 |
import PyPDF2
|
|
|
|
| 16 |
# =========================================
|
| 17 |
|
| 18 |
MODEL_OPTIONS = {
|
| 19 |
+
"Zephyr 7B (Serverless)": "HuggingFaceH4/zephyr-7b-beta",
|
| 20 |
+
"Mixtral 8x7B (Serverless)": "mistralai/Mixtral-8x7B-Instruct-v0.1",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
}
|
| 22 |
|
| 23 |
+
FALLBACK_MODEL = "HuggingFaceH4/zephyr-7b-beta"
|
|
|
|
| 24 |
|
| 25 |
PERSONA_PRESETS = {
|
| 26 |
"Balanced Assistant": "You are a helpful, intelligent AI assistant.",
|
|
|
|
| 49 |
7. Limitations
|
| 50 |
Material:
|
| 51 |
{user_input}
|
| 52 |
+
""",
|
| 53 |
+
|
| 54 |
+
"Explain Code (Natural Voice)": """
|
| 55 |
+
Explain the following code clearly and conversationally,
|
| 56 |
+
as if teaching a developer step-by-step in a calm, natural voice.
|
| 57 |
+
|
| 58 |
+
Code:
|
| 59 |
+
{user_input}
|
| 60 |
"""
|
| 61 |
}
|
| 62 |
|
|
|
|
| 73 |
token = os.environ.get("HF_TOKEN")
|
| 74 |
if not token:
|
| 75 |
raise RuntimeError("HF_TOKEN not set in Space Secrets.")
|
|
|
|
| 76 |
return InferenceClient(model=model_id, token=token)
|
| 77 |
|
| 78 |
|
|
|
|
| 97 |
return ""
|
| 98 |
|
| 99 |
|
| 100 |
+
def text_to_speech(text):
|
| 101 |
+
tts = gTTS(text=text)
|
| 102 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
| 103 |
+
tts.save(temp_file.name)
|
| 104 |
+
return temp_file.name
|
| 105 |
+
|
| 106 |
+
|
| 107 |
def run_model(client, messages, temperature):
|
| 108 |
response = client.chat_completion(
|
| 109 |
messages=messages,
|
|
|
|
| 119 |
# Core Logic
|
| 120 |
# =========================================
|
| 121 |
|
| 122 |
+
def generate_response(message, history, model_label, persona_label, task_mode, uploaded_file, voice_enabled):
|
| 123 |
|
| 124 |
if not message:
|
| 125 |
+
return history, None
|
| 126 |
|
| 127 |
try:
|
| 128 |
message = message[:MAX_INPUT_CHARS]
|
| 129 |
history = history[-MAX_HISTORY_PAIRS * 2:]
|
| 130 |
|
| 131 |
+
model_id = MODEL_OPTIONS[model_label]
|
| 132 |
+
system_prompt = PERSONA_PRESETS.get(persona_label, "")
|
|
|
|
|
|
|
| 133 |
temperature = 0.4 if "Forensic" in persona_label else 0.7
|
| 134 |
|
| 135 |
file_text = extract_text(uploaded_file)
|
|
|
|
| 143 |
formatted_input = TASK_MODES[task_mode].format(user_input=message)
|
| 144 |
messages.append({"role": "user", "content": formatted_input})
|
| 145 |
|
|
|
|
| 146 |
try:
|
| 147 |
client = get_client(model_id)
|
| 148 |
answer = run_model(client, messages, temperature)
|
|
|
|
|
|
|
| 149 |
except Exception:
|
| 150 |
+
fallback_client = get_client(FALLBACK_MODEL)
|
| 151 |
+
answer = "⚠️ Fallback model used.\n\n" + run_model(fallback_client, messages, temperature)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
|
| 153 |
history.append({"role": "user", "content": message})
|
| 154 |
history.append({"role": "assistant", "content": answer})
|
| 155 |
|
| 156 |
+
audio_file = None
|
| 157 |
+
if voice_enabled:
|
| 158 |
+
audio_file = text_to_speech(answer)
|
| 159 |
+
|
| 160 |
+
return history, audio_file
|
| 161 |
|
| 162 |
except Exception as e:
|
| 163 |
history.append({"role": "assistant", "content": f"⚠️ ERROR:\n\n{str(e)}"})
|
| 164 |
+
return history, None
|
| 165 |
|
| 166 |
|
| 167 |
# =========================================
|
|
|
|
| 169 |
# =========================================
|
| 170 |
|
| 171 |
def clear_chat():
|
| 172 |
+
return [], None
|
| 173 |
|
| 174 |
def export_chat(history):
|
| 175 |
file_path = "conversation.json"
|
|
|
|
| 184 |
|
| 185 |
with gr.Blocks(theme=gr.themes.Soft(), title="Omniscient IRIS") as demo:
|
| 186 |
|
| 187 |
+
gr.Markdown("## Omniscient IRIS — Voice Enabled")
|
| 188 |
|
| 189 |
with gr.Row():
|
| 190 |
|
|
|
|
| 196 |
show_label=False
|
| 197 |
)
|
| 198 |
|
| 199 |
+
audio_output = gr.Audio(autoplay=True)
|
| 200 |
+
|
| 201 |
send_btn = gr.Button("Send", variant="primary")
|
| 202 |
clear_btn = gr.Button("Clear")
|
| 203 |
|
|
|
|
| 205 |
|
| 206 |
model_selector = gr.Dropdown(
|
| 207 |
choices=list(MODEL_OPTIONS.keys()),
|
| 208 |
+
value=list(MODEL_OPTIONS.keys())[0],
|
| 209 |
label="Model"
|
| 210 |
)
|
| 211 |
|
|
|
|
| 218 |
task_selector = gr.Dropdown(
|
| 219 |
choices=list(TASK_MODES.keys()),
|
| 220 |
value="General Chat",
|
| 221 |
+
label="Mode"
|
| 222 |
)
|
| 223 |
|
| 224 |
+
voice_toggle = gr.Checkbox(label="Enable Voice Output", value=True)
|
| 225 |
+
|
| 226 |
file_upload = gr.File(
|
| 227 |
label="Upload Document (txt, pdf, md)",
|
| 228 |
file_types=[".txt", ".pdf", ".md"]
|
|
|
|
| 234 |
|
| 235 |
send_btn.click(
|
| 236 |
generate_response,
|
| 237 |
+
inputs=[msg, state, model_selector, persona_selector, task_selector, file_upload, voice_toggle],
|
| 238 |
+
outputs=[chatbot, audio_output],
|
| 239 |
)
|
| 240 |
|
| 241 |
msg.submit(
|
| 242 |
generate_response,
|
| 243 |
+
inputs=[msg, state, model_selector, persona_selector, task_selector, file_upload, voice_toggle],
|
| 244 |
+
outputs=[chatbot, audio_output],
|
| 245 |
)
|
| 246 |
|
| 247 |
clear_btn.click(
|
| 248 |
fn=clear_chat,
|
| 249 |
+
outputs=[chatbot, audio_output],
|
| 250 |
).then(lambda: [], outputs=state)
|
| 251 |
|
| 252 |
export_btn.click(
|