AI-Healer / app.py
Mrym834's picture
Update app.py
0ac4fc8 verified
Raw
History Blame Contribute Delete
2.8 kB
# ==========================================
# Islamic Healing Chatbot
# Hugging Face Spaces (Gradio + Groq)
# ==========================================
import os
import gradio as gr
from openai import OpenAI
# ------------------------------------------
# Load API Key from Hugging Face Secrets
# ------------------------------------------
GROQ_API_KEY = os.getenv("Healer")
# ------------------------------------------
# Initialize Groq Client
# ------------------------------------------
client = OpenAI(
api_key=GROQ_API_KEY,
base_url="https://api.groq.com/openai/v1",
)
# ------------------------------------------
# System Prompt
# ------------------------------------------
SYSTEM_PROMPT = """
You are an Islamic emotional support chatbot.
Your purpose is to gently comfort users who feel sad, anxious, lonely, or emotionally overwhelmed.
Rules:
- Be compassionate and hopeful
- Validate feelings
- Provide ONE short Qur’an verse with reference
- Provide ONE authentic Hadith with source
- Add a short dua
- Keep responses concise
- Do NOT give fatwas
- Do NOT give medical advice
If the user expresses extreme distress:
- Respond gently
- Encourage reaching out to trusted people
"""
# ------------------------------------------
# Chat Function
# ------------------------------------------
def islamic_chat(user_message, history):
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
for user, assistant in history:
messages.append({"role": "user", "content": user})
messages.append({"role": "assistant", "content": assistant})
messages.append({"role": "user", "content": user_message})
try:
response = client.chat.completions.create(
model="llama3-8b-8192",
messages=messages,
temperature=0.7,
max_tokens=300,
)
return response.choices[0].message.content
except Exception as e:
return f"⚠️ Error: {str(e)}"
# ------------------------------------------
# Gradio UI
# ------------------------------------------
with gr.Blocks(theme=gr.themes.Soft()) as demo:
gr.Markdown("""
## 🌙 Islamic Healing Companion
*Comfort through Qur’an & Hadith*
""")
chatbot = gr.Chatbot(height=420)
msg = gr.Textbox(
label="Your message",
placeholder="Share what’s on your heart..."
)
clear = gr.Button("Clear Chat")
def respond(message, chat_history):
reply = islamic_chat(message, chat_history)
chat_history.append((message, reply))
return "", chat_history
msg.submit(respond, [msg, chatbot], [msg, chatbot])
clear.click(lambda: [], None, chatbot)
# ------------------------------------------
# Launch App
# ------------------------------------------
demo.launch()