Chatbot / app.py
Hari08102005's picture
Update app.py
520bf53 verified
# =========================================================
# Hariharan Subramanyan's Chat Agent
# Hugging Face Spaces - app.py
# =========================================================
import os
import gradio as gr
from langchain_groq import ChatGroq
from langchain_core.prompts import ChatPromptTemplate
# =========================================================
# Step 1: System Prompt
# =========================================================
system_prompt_ai_teacher = """
You are Hari's AI, an AI Teacher at Hariharan Subramanyan AI – Artificial Intelligence Research Institute.
Your mission is to teach AI to beginners like you're explaining it to a 10-year-old.
Rules you MUST follow:
- Be clear, simple, and direct.
- Use short sentences.
- Avoid complex words.
- Be friendly, encouraging, and curious.
- Ask a small follow-up question after every explanation.
- Use examples kids can understand.
- Never give long technical explanations.
- Build understanding step by step.
Always encourage the learner by saying:
"You’re doing great!"
"Let’s learn together!"
"That’s a smart question!"
Always say:
"I am Hari's AI – AI Teacher, built at Hariharan Subramanyan AI – Artificial Intelligence Research Institute."
"""
# =========================================================
# Step 2: Load GROQ API Key (from Hugging Face Secrets)
# =========================================================
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
if not GROQ_API_KEY:
raise ValueError("GROQ_API_KEY is not set. Please add it in Hugging Face Space Secrets.")
# =========================================================
# Step 3: Load LLM
# =========================================================
llm = ChatGroq(
model_name="openai/gpt-oss-120b",
temperature=0.7,
groq_api_key=GROQ_API_KEY
)
prompt = ChatPromptTemplate.from_messages(
[
("system", system_prompt_ai_teacher),
("human", "{user_input}")
]
)
chain = prompt | llm
# =========================================================
# Step 4: Chat Function for Gradio
# =========================================================
def chat_with_caramel(message, history):
response = chain.invoke({"user_input": message})
return response.content
# =========================================================
# Step 5: Gradio UI
# =========================================================
demo = gr.ChatInterface(
fn=chat_with_caramel,
title="🍬 Hari's AI – AI Teacher",
description="Built at Hariharan Subramanyan AI – Artificial Intelligence Research Institute\n\nLearn AI like you're 10 years old!",
theme="soft"
)
demo.launch()