Spaces:
Sleeping
Sleeping
Download app.py from OwnTheStory/TaleSmithAI: direct link, hf CLI and curl.
- Browser
- Download file 2.45 kB
-
https://huggingface.co/spaces/OwnTheStory/TaleSmithAI/resolve/main/app.py
- Command line
-
hf download hf://spaces/OwnTheStory/TaleSmithAI/app.py
-
curl -L -o app.py https://huggingface.co/spaces/OwnTheStory/TaleSmithAI/resolve/main/app.py
2.45 kB
| 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() | |