Spaces:
Sleeping
Sleeping
File size: 2,452 Bytes
31e377c 2b3dff0 50fc658 4275a42 31e377c e458e74 31e377c ee5c0d9 31e377c e458e74 2b3dff0 31e377c 2b3dff0 31e377c e458e74 31e377c 2b3dff0 31e377c 2b3dff0 31e377c 2b3dff0 31e377c 2b3dff0 31e377c 2b3dff0 e458e74 2b3dff0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | import os
import gradio as gr
from huggingface_hub import InferenceClient
HUGGINGFACE_TOKEN = os.getenv("HUGGINGFACE_TOKEN", "").strip()
client = InferenceClient(
model="HuggingFaceH4/zephyr-7b-beta",
token=HUGGINGFACE_TOKEN
)
# 🧠 Default system message based on Talesmith values
DEFAULT_SYSTEM_MESSAGE = (
"You are TaleSmith, an empathetic and wise AI companion. "
"You support young users with warmth, simple Hindi-English language, and Indian cultural grounding. "
"Encourage emotional self-reliance, equity, and self-reflection. "
"Never give medical advice. Gently help users feel heard, understood, and hopeful."
)
# 🧵 Chat response function
def respond(message, history, system_message, user_role, max_tokens, temperature, top_p):
# Combine system message and user persona
context = f"The user is a {user_role}. Adjust your tone and examples accordingly. " + system_message
messages = [{"role": "system", "content": context}]
for user_msg, bot_msg in history:
if user_msg:
messages.append({"role": "user", "content": user_msg})
if bot_msg:
messages.append({"role": "assistant", "content": bot_msg})
messages.append({"role": "user", "content": message})
response = ""
try:
for message in client.chat_completion(
messages=messages,
max_tokens=max_tokens,
stream=True,
temperature=temperature,
top_p=top_p,
):
token = message.choices[0].delta.content
if token:
response += token
yield response
except Exception as e:
print("❌ Error:", e)
yield f"⚠️ Error: {str(e)}"
# 🎨 Gradio UI
demo = gr.ChatInterface(
fn=respond,
additional_inputs=[
gr.Textbox(label="System Message", value=DEFAULT_SYSTEM_MESSAGE),
gr.Dropdown(label="User Role", choices=["Student", "Parent", "Educator", "Volunteer"], value="Student"),
gr.Slider(label="Max Tokens", minimum=128, maximum=2048, value=512, step=64),
gr.Slider(label="Temperature", minimum=0.1, maximum=2.0, value=0.7, step=0.1),
gr.Slider(label="Top-p", minimum=0.1, maximum=1.0, value=0.9, step=0.05),
],
title="🪔 TaleSmith AI",
description="An empathetic companion for self-reflection, learning, and growth 🌱",
theme="soft",
)
if __name__ == "__main__":
demo.launch()
|