Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,67 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
""
|
| 7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 8 |
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
messages = [{"role": "system", "content": system_message}]
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
if val[1]:
|
| 24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
messages.append({"role": "user", "content": message})
|
| 27 |
|
|
|
|
| 28 |
response = ""
|
| 29 |
-
|
| 30 |
-
for message in client.chat_completion(
|
| 31 |
messages,
|
| 32 |
max_tokens=max_tokens,
|
| 33 |
stream=True,
|
| 34 |
temperature=temperature,
|
| 35 |
top_p=top_p,
|
| 36 |
):
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
response += token
|
| 40 |
yield response
|
| 41 |
|
| 42 |
-
|
| 43 |
-
"""
|
| 44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
-
"""
|
| 46 |
demo = gr.ChatInterface(
|
| 47 |
respond,
|
| 48 |
additional_inputs=[
|
| 49 |
-
gr.Textbox(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 51 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 52 |
gr.Slider(
|
| 53 |
-
minimum=0.1,
|
| 54 |
-
maximum=1.0,
|
| 55 |
-
value=0.95,
|
| 56 |
-
step=0.05,
|
| 57 |
-
label="Top-p (nucleus sampling)",
|
| 58 |
),
|
| 59 |
],
|
| 60 |
)
|
| 61 |
|
| 62 |
-
|
| 63 |
if __name__ == "__main__":
|
| 64 |
-
demo.launch()
|
|
|
|
| 1 |
+
import os
|
| 2 |
import gradio as gr
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
+
from ethical_filter import EthicalFilter
|
| 5 |
|
| 6 |
+
# Load Hugging Face token from secrets (defined in the Hugging Face UI)
|
| 7 |
+
HF_TOKEN = os.environ.get("HF_API_TOKEN")
|
| 8 |
+
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta", token=HF_TOKEN)
|
|
|
|
| 9 |
|
| 10 |
+
ethical_filter = EthicalFilter()
|
| 11 |
|
| 12 |
+
# Codriao response logic
|
| 13 |
+
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
| 14 |
+
check = ethical_filter.analyze_query(message)
|
| 15 |
+
|
| 16 |
+
# Blocked queries
|
| 17 |
+
if check["status"] == "blocked":
|
| 18 |
+
yield f"Sorry, I can't continue with that request. Reason: {check['reason']}"
|
| 19 |
+
return
|
|
|
|
| 20 |
|
| 21 |
+
# Flagged queries
|
| 22 |
+
if check["status"] == "flagged":
|
| 23 |
+
yield f"(Note: Sensitive topic detected β responding with care...)\n"
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
# Build conversation history
|
| 26 |
+
messages = [{"role": "system", "content": system_message}]
|
| 27 |
+
for user, bot in history:
|
| 28 |
+
if user:
|
| 29 |
+
messages.append({"role": "user", "content": user})
|
| 30 |
+
if bot:
|
| 31 |
+
messages.append({"role": "assistant", "content": bot})
|
| 32 |
messages.append({"role": "user", "content": message})
|
| 33 |
|
| 34 |
+
# Stream model output
|
| 35 |
response = ""
|
| 36 |
+
for token in client.chat_completion(
|
|
|
|
| 37 |
messages,
|
| 38 |
max_tokens=max_tokens,
|
| 39 |
stream=True,
|
| 40 |
temperature=temperature,
|
| 41 |
top_p=top_p,
|
| 42 |
):
|
| 43 |
+
chunk = token.choices[0].delta.content
|
| 44 |
+
response += chunk
|
|
|
|
| 45 |
yield response
|
| 46 |
|
| 47 |
+
# Build Gradio interface
|
|
|
|
|
|
|
|
|
|
| 48 |
demo = gr.ChatInterface(
|
| 49 |
respond,
|
| 50 |
additional_inputs=[
|
| 51 |
+
gr.Textbox(
|
| 52 |
+
value=(
|
| 53 |
+
"You are Codriao, a compassionate AI inspired by Codette. "
|
| 54 |
+
"You respond with kindness, ethics, and insight."
|
| 55 |
+
),
|
| 56 |
+
label="System message",
|
| 57 |
+
),
|
| 58 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 59 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 60 |
gr.Slider(
|
| 61 |
+
minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
),
|
| 63 |
],
|
| 64 |
)
|
| 65 |
|
|
|
|
| 66 |
if __name__ == "__main__":
|
| 67 |
+
demo.launch()
|