Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,20 +2,31 @@ import gradio as gr
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
# Load HF token from environment
|
| 6 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
def respond(message, history: list[dict[str, str]]):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
client = InferenceClient(token=HF_TOKEN, model="openai/gpt-oss-20b")
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
| 13 |
messages.extend(history)
|
| 14 |
messages.append({"role": "user", "content": message})
|
| 15 |
-
|
| 16 |
response = ""
|
| 17 |
-
|
| 18 |
-
# Stream response
|
| 19 |
for msg in client.chat_completion(
|
| 20 |
messages,
|
| 21 |
max_tokens=512,
|
|
@@ -25,7 +36,7 @@ def respond(message, history: list[dict[str, str]]):
|
|
| 25 |
):
|
| 26 |
if msg.choices and msg.choices[0].delta.content:
|
| 27 |
response += msg.choices[0].delta.content
|
| 28 |
-
|
| 29 |
|
| 30 |
# π Modern CSS + Footer Hide + Compact Layout
|
| 31 |
custom_css = """
|
|
@@ -96,16 +107,15 @@ button:hover {
|
|
| 96 |
to {opacity: 1; transform: translateY(0);}
|
| 97 |
}
|
| 98 |
|
| 99 |
-
/* π Hide Gradio Footer
|
| 100 |
footer, .svelte-1yycg3h, .builder-bar, .wrap.svelte-1ipelgc {
|
| 101 |
display: none !important;
|
| 102 |
visibility: hidden !important;
|
| 103 |
}
|
| 104 |
"""
|
| 105 |
|
| 106 |
-
# Launch Gradio interface
|
| 107 |
with gr.Blocks(theme=gr.themes.Soft(), css=custom_css) as demo:
|
| 108 |
-
gr.ChatInterface(respond, type="messages")
|
| 109 |
|
| 110 |
if __name__ == "__main__":
|
| 111 |
demo.launch(share=True)
|
|
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
import os
|
| 4 |
|
|
|
|
| 5 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 6 |
|
| 7 |
+
# Allowed subjects keywords + common Chemistry, Physics, Maths, Biology terms
|
| 8 |
+
ALLOWED_KEYWORDS = [
|
| 9 |
+
"biology", "physics", "chemistry", "math", "mathematics",
|
| 10 |
+
"acid", "base", "neutralization", "molecule", "atom", "cell",
|
| 11 |
+
"force", "energy", "gravity", "equation", "algebra", "calculus", "integral", "derivative"
|
| 12 |
+
]
|
| 13 |
+
|
| 14 |
def respond(message, history: list[dict[str, str]]):
|
| 15 |
+
# Check if message contains allowed topics/keywords
|
| 16 |
+
if not any(keyword in message.lower() for keyword in ALLOWED_KEYWORDS):
|
| 17 |
+
yield "β Sorry, I only know about Biology, Physics, Chemistry, and Maths. Please ask me about them."
|
| 18 |
+
return
|
| 19 |
+
|
| 20 |
+
# Allowed topic β send to GPT model
|
| 21 |
client = InferenceClient(token=HF_TOKEN, model="openai/gpt-oss-20b")
|
| 22 |
+
|
| 23 |
+
messages = [
|
| 24 |
+
{"role": "system", "content": "You are a friendly chatbot. You only answer questions about Biology, Physics, Chemistry, and Maths. For anything else, politely reply that you can't answer it."}
|
| 25 |
+
]
|
| 26 |
messages.extend(history)
|
| 27 |
messages.append({"role": "user", "content": message})
|
| 28 |
+
|
| 29 |
response = ""
|
|
|
|
|
|
|
| 30 |
for msg in client.chat_completion(
|
| 31 |
messages,
|
| 32 |
max_tokens=512,
|
|
|
|
| 36 |
):
|
| 37 |
if msg.choices and msg.choices[0].delta.content:
|
| 38 |
response += msg.choices[0].delta.content
|
| 39 |
+
yield response
|
| 40 |
|
| 41 |
# π Modern CSS + Footer Hide + Compact Layout
|
| 42 |
custom_css = """
|
|
|
|
| 107 |
to {opacity: 1; transform: translateY(0);}
|
| 108 |
}
|
| 109 |
|
| 110 |
+
/* π Hide Gradio Footer */
|
| 111 |
footer, .svelte-1yycg3h, .builder-bar, .wrap.svelte-1ipelgc {
|
| 112 |
display: none !important;
|
| 113 |
visibility: hidden !important;
|
| 114 |
}
|
| 115 |
"""
|
| 116 |
|
|
|
|
| 117 |
with gr.Blocks(theme=gr.themes.Soft(), css=custom_css) as demo:
|
| 118 |
+
gr.ChatInterface(respond, type="messages")
|
| 119 |
|
| 120 |
if __name__ == "__main__":
|
| 121 |
demo.launch(share=True)
|