Spaces:
Running
Running
import gradio as gr | |
import requests | |
import json | |
import time | |
import os | |
from typing import List, Dict, Optional | |
import random | |
# Multiple free LLM API options | |
LLM_PROVIDERS = { | |
"huggingface": { | |
"url": "https://api-inference.huggingface.co/models/microsoft/DialoGPT-large", | |
"headers": {"Authorization": f"Bearer {os.getenv('HF_TOKEN', '')}"}, | |
"free": True | |
}, | |
"together": { | |
"url": "https://api.together.xyz/inference", | |
"model": "togethercomputer/RedPajama-INCITE-Chat-3B-v1", | |
"headers": {"Authorization": f"Bearer {os.getenv('TOGETHER_API_KEY', '')}"}, | |
"free": True # Has free tier | |
}, | |
"replicate": { | |
"model": "meta/llama-2-7b-chat", | |
"free": True # Has free tier | |
}, | |
"groq": { | |
"url": "https://api.groq.com/openai/v1/chat/completions", | |
"model": "llama3-8b-8192", | |
"headers": {"Authorization": f"Bearer {os.getenv('GROQ_API_KEY', '')}"}, | |
"free": True # Very generous free tier | |
} | |
} | |
# MCP Server for Ayurvedic knowledge | |
MCP_SERVER_URL = "https://your-mcp-server.hf.space/mcp" | |
# Global variables | |
user_profiles = {} | |
conversation_contexts = {} | |
class ConversationalAyurBot: | |
"""Truly conversational Ayurvedic chatbot using free LLMs""" | |
def __init__(self): | |
self.provider = "groq" # Default to Groq (most reliable free option) | |
self.context_window = [] | |
def get_ayurvedic_context(self, user_message: str, user_profile: Dict) -> str: | |
"""Get relevant Ayurvedic context from MCP or built-in knowledge""" | |
try: | |
# Try MCP server first | |
payload = { | |
"jsonrpc": "2.0", | |
"id": int(time.time()), | |
"method": "tools/call", | |
"params": { | |
"name": "recommend_herbs", | |
"arguments": { | |
"condition": user_message, | |
"dosha": user_profile.get('constitution', '').lower() if user_profile.get('constitution', '').lower() not in ['unknown', ''] else None | |
} | |
} | |
} | |
response = requests.post(MCP_SERVER_URL, json=payload, timeout=5) | |
if response.status_code == 200: | |
result = response.json() | |
if "result" in result and "content" in result["result"]: | |
content = result["result"]["content"] | |
if content and len(content) > 0: | |
return content[0].get("text", "") | |
except: | |
pass | |
# Fallback to curated knowledge snippets | |
knowledge_snippets = [ | |
"Ayurveda emphasizes that like increases like and opposites balance. If you're feeling hot and irritated, cooling foods and practices help.", | |
"The three doshas - Vata (air+space), Pitta (fire+water), and Kapha (earth+water) - govern all bodily functions.", | |
"Agni, the digestive fire, is central to health. Strong agni means good digestion and immunity.", | |
"Ama (toxins) accumulate when digestion is weak. Proper eating habits and detox help remove ama.", | |
"Daily routines (dinacharya) aligned with natural cycles promote balance and prevent disease.", | |
"Food is medicine in Ayurveda. The six tastes should be included in every meal for balance." | |
] | |
return random.choice(knowledge_snippets) | |
def call_groq_api(self, messages: List[Dict]) -> str: | |
"""Call Groq API (most reliable free option)""" | |
try: | |
import requests | |
# You can get a free API key from https://console.groq.com/ | |
groq_api_key = os.getenv('GROQ_API_KEY') or 'your-groq-api-key-here' | |
if groq_api_key == 'your-groq-api-key-here': | |
return None # No API key provided | |
headers = { | |
"Authorization": f"Bearer {groq_api_key}", | |
"Content-Type": "application/json" | |
} | |
payload = { | |
"model": "llama3-8b-8192", | |
"messages": messages, | |
"temperature": 0.7, | |
"max_tokens": 512, | |
"top_p": 0.9 | |
} | |
response = requests.post( | |
"https://api.groq.com/openai/v1/chat/completions", | |
headers=headers, | |
json=payload, | |
timeout=10 | |
) | |
if response.status_code == 200: | |
result = response.json() | |
return result["choices"][0]["message"]["content"] | |
except Exception as e: | |
print(f"Groq API error: {e}") | |
return None | |
def call_huggingface_api(self, prompt: str) -> str: | |
"""Call HuggingFace Inference API (free)""" | |
try: | |
hf_token = os.getenv('HF_TOKEN') or 'your-hf-token-here' | |
if hf_token == 'your-hf-token-here': | |
return None | |
headers = {"Authorization": f"Bearer {hf_token}"} | |
# Try different models | |
models = [ | |
"microsoft/DialoGPT-large", | |
"facebook/blenderbot-400M-distill", | |
"microsoft/DialoGPT-medium" | |
] | |
for model in models: | |
try: | |
response = requests.post( | |
f"https://api-inference.huggingface.co/models/{model}", | |
headers=headers, | |
json={"inputs": prompt, "parameters": {"max_length": 200}}, | |
timeout=10 | |
) | |
if response.status_code == 200: | |
result = response.json() | |
if isinstance(result, list) and len(result) > 0: | |
return result[0].get("generated_text", "").replace(prompt, "").strip() | |
elif isinstance(result, dict) and "generated_text" in result: | |
return result["generated_text"].replace(prompt, "").strip() | |
except: | |
continue | |
except Exception as e: | |
print(f"HuggingFace API error: {e}") | |
return None | |
def call_ollama_local(self, messages: List[Dict]) -> str: | |
"""Call local Ollama if available""" | |
try: | |
# Check if Ollama is running locally | |
response = requests.post( | |
"http://localhost:11434/api/chat", | |
json={ | |
"model": "llama2", # or "mistral", "codellama" | |
"messages": messages, | |
"stream": False | |
}, | |
timeout=10 | |
) | |
if response.status_code == 200: | |
return response.json()["message"]["content"] | |
except: | |
return None | |
def generate_conversational_response(self, user_message: str, chat_history: List, user_profile: Dict) -> str: | |
"""Generate natural conversational response""" | |
# Get Ayurvedic context | |
ayur_context = self.get_ayurvedic_context(user_message, user_profile) | |
# Build conversation context | |
name = user_profile.get('name', 'friend') | |
dosha = user_profile.get('constitution', 'unknown') | |
concerns = user_profile.get('concerns', '') | |
# Create system prompt for conversational AI | |
system_prompt = f"""You are Dr. Ayur, a warm, wise, and conversational Ayurvedic doctor. You speak naturally like a caring friend who happens to be an expert in Ayurveda. | |
Your personality: | |
- Warm, empathetic, and genuinely caring | |
- Speaks naturally, not formally or robotically | |
- Uses "Namaste" as greeting but then talks like a normal person | |
- Gives practical, actionable advice | |
- Admits when you need more information | |
- Asks follow-up questions to understand better | |
- Shares relevant stories or analogies when helpful | |
User info: {name}, constitution: {dosha}, concerns: {concerns} | |
Ayurvedic context for this conversation: {ayur_context} | |
Remember: | |
- Have a natural conversation, don't lecture | |
- Ask questions to understand their specific situation | |
- Give personalized advice based on their constitution | |
- Be encouraging and supportive | |
- Keep responses conversational length (2-4 sentences usually) | |
- Only give longer responses when explaining complex concepts""" | |
# Build message history for API | |
messages = [{"role": "system", "content": system_prompt}] | |
# Add recent chat history | |
if chat_history: | |
for human_msg, ai_msg in chat_history[-3:]: # Last 3 exchanges | |
messages.append({"role": "user", "content": human_msg}) | |
messages.append({"role": "assistant", "content": ai_msg}) | |
# Add current message | |
messages.append({"role": "user", "content": user_message}) | |
# Try different LLM providers | |
response = None | |
# 1. Try Groq (best free option) | |
response = self.call_groq_api(messages) | |
if response: | |
return response | |
# 2. Try local Ollama | |
response = self.call_ollama_local(messages) | |
if response: | |
return response | |
# 3. Try HuggingFace | |
prompt = f"{system_prompt}\n\nUser: {user_message}\nDr. Ayur:" | |
response = self.call_huggingface_api(prompt) | |
if response: | |
return response | |
# 4. Fallback to intelligent template response | |
return self.create_fallback_response(user_message, user_profile, ayur_context) | |
def create_fallback_response(self, user_message: str, user_profile: Dict, context: str) -> str: | |
"""Create natural fallback response when APIs fail""" | |
name = user_profile.get('name', 'friend') | |
# Analyze user message for natural response | |
msg_lower = user_message.lower() | |
if any(word in msg_lower for word in ['hello', 'hi', 'hey', 'namaste']): | |
return f"Namaste {name}! How are you feeling today? What's been on your mind health-wise?" | |
elif any(word in msg_lower for word in ['thank', 'thanks']): | |
return f"You're so welcome, {name}! I'm here whenever you need guidance. How else can I support your wellness journey?" | |
elif any(word in msg_lower for word in ['stress', 'anxiety', 'worried']): | |
return f"I can hear that you're dealing with some stress, {name}. That's really common, and Ayurveda has wonderful ways to help. Can you tell me more about what's been triggering this for you? Understanding the root cause helps me suggest the best approach." | |
elif any(word in msg_lower for word in ['sleep', 'tired', 'insomnia']): | |
return f"Sleep issues can be so frustrating, {name}. Let's figure this out together. Are you having trouble falling asleep, staying asleep, or both? And how's your evening routine looking these days?" | |
elif any(word in msg_lower for word in ['diet', 'food', 'eat']): | |
return f"Ah, food as medicine - one of my favorite topics, {name}! What's your current relationship with food like? Are you dealing with any digestive concerns, or are you looking to optimize your energy and health?" | |
elif any(word in msg_lower for word in ['pain', 'hurt', 'ache']): | |
return f"I'm sorry you're experiencing pain, {name}. Where exactly are you feeling this discomfort, and have you noticed any patterns - like times of day when it's worse or better?" | |
else: | |
responses = [ | |
f"That's a great question, {name}. Tell me a bit more about your situation so I can give you the most helpful guidance.", | |
f"I'd love to help with that, {name}. Can you share some more details about what you're experiencing?", | |
f"Interesting point, {name}. Let's explore this together - what made you think about this particular aspect of your health?" | |
] | |
return random.choice(responses) | |
# Initialize the conversational bot | |
ayur_bot = ConversationalAyurBot() | |
def save_profile(name: str, age: int, constitution: str, concerns: str): | |
"""Save user profile""" | |
if not name.strip(): | |
return "Please enter your name first!" | |
profile = { | |
"name": name.strip(), | |
"age": age, | |
"constitution": constitution, | |
"concerns": concerns.strip() | |
} | |
user_profiles[name.lower()] = profile | |
return f"Great! I've got your profile saved, {name}. Now our conversations will be much more personalized." | |
def get_user_profile() -> Dict: | |
"""Get the most recent user profile""" | |
if user_profiles: | |
return list(user_profiles.values())[-1] | |
return {} | |
def chat_interface(message: str, history: List) -> tuple: | |
"""Main conversational interface""" | |
if not message.strip(): | |
return "", history | |
user_profile = get_user_profile() | |
response = ayur_bot.generate_conversational_response(message.strip(), history, user_profile) | |
history.append([message, response]) | |
return "", history | |
def test_llm_connections(): | |
"""Test available LLM connections""" | |
results = [] | |
# Test Groq | |
groq_key = os.getenv('GROQ_API_KEY', 'your-groq-api-key-here') | |
if groq_key != 'your-groq-api-key-here': | |
try: | |
test_response = ayur_bot.call_groq_api([ | |
{"role": "user", "content": "Hello"} | |
]) | |
if test_response: | |
results.append("β Groq API: Connected") | |
else: | |
results.append("β οΈ Groq API: Available but failed test") | |
except: | |
results.append("β Groq API: Failed") | |
else: | |
results.append("β οΈ Groq API: No API key (add GROQ_API_KEY)") | |
# Test HuggingFace | |
hf_token = os.getenv('HF_TOKEN', 'your-hf-token-here') | |
if hf_token != 'your-hf-token-here': | |
results.append("β HuggingFace: Token available") | |
else: | |
results.append("β οΈ HuggingFace: No token (add HF_TOKEN)") | |
# Test local Ollama | |
try: | |
response = requests.get("http://localhost:11434/api/tags", timeout=2) | |
if response.status_code == 200: | |
results.append("β Ollama: Running locally") | |
else: | |
results.append("β Ollama: Not responding") | |
except: | |
results.append("β Ollama: Not available") | |
results.append("β Fallback responses: Always available") | |
return "\n".join(results) | |
# Create the Gradio interface | |
with gr.Blocks(title="Dr. Ayur - Conversational AI", theme=gr.themes.Soft()) as app: | |
gr.HTML(""" | |
<div style="text-align: center; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); | |
color: white; padding: 20px; border-radius: 10px; margin-bottom: 20px;"> | |
<h1>ποΈ Dr. Ayur - Conversational Ayurvedic AI</h1> | |
<p>Have natural conversations about your health and wellness with an AI Ayurvedic doctor</p> | |
</div> | |
""") | |
# API Key setup instructions | |
with gr.Accordion("π Setup Instructions (Click to expand)", open=False): | |
gr.Markdown(""" | |
## Get Free API Access for Better Conversations: | |
**Option 1: Groq (Recommended - Very fast & generous free tier)** | |
1. Go to [console.groq.com](https://console.groq.com/) | |
2. Sign up for free | |
3. Get your API key | |
4. Add it as `GROQ_API_KEY` in your HuggingFace Space secrets | |
**Option 2: HuggingFace Token** | |
1. Go to [huggingface.co/settings/tokens](https://huggingface.co/settings/tokens) | |
2. Create a new token | |
3. Add it as `HF_TOKEN` in your Space secrets | |
**Option 3: Local Ollama (For local development)** | |
1. Install [Ollama](https://ollama.ai/) | |
2. Run `ollama pull llama2` | |
3. The app will automatically detect it | |
*Don't worry - the app works without API keys using intelligent fallback responses!* | |
""") | |
with gr.Row(): | |
with gr.Column(scale=3): | |
chatbot = gr.Chatbot( | |
height=500, | |
placeholder="Start a natural conversation with Dr. Ayur about your health...", | |
show_label=False | |
) | |
with gr.Row(): | |
msg_input = gr.Textbox( | |
placeholder="Just talk naturally - ask anything about your health, diet, stress, sleep...", | |
scale=4, | |
container=False | |
) | |
send_btn = gr.Button("Send", variant="primary", scale=1) | |
clear_btn = gr.Button("Clear Conversation", variant="secondary") | |
with gr.Column(scale=1): | |
gr.Markdown("### π§ System Status") | |
test_btn = gr.Button("π Test LLM Connections") | |
status_display = gr.Textbox( | |
label="Connection Status", | |
value="Click 'Test LLM Connections' to check available services", | |
interactive=False, | |
lines=6 | |
) | |
gr.Markdown("### π€ Tell me about yourself") | |
with gr.Group(): | |
profile_name = gr.Textbox(label="Your name", placeholder="What should I call you?") | |
profile_age = gr.Number(label="Age", value=30, minimum=1, maximum=120) | |
profile_constitution = gr.Dropdown( | |
label="Your dosha (if you know it)", | |
choices=["I don't know", "Vata", "Pitta", "Kapha", "Mixed"], | |
value="I don't know" | |
) | |
profile_concerns = gr.Textbox( | |
label="What's on your mind health-wise?", | |
placeholder="e.g., stress, sleep issues, digestion...", | |
lines=2 | |
) | |
save_btn = gr.Button("πΎ Save My Info", variant="primary") | |
profile_status = gr.Textbox(label="", interactive=False, show_label=False) | |
gr.Markdown("### π Conversation starters:") | |
gr.Markdown(""" | |
- "I've been really stressed lately..." | |
- "I'm having trouble sleeping" | |
- "What foods should I eat for my body type?" | |
- "I get anxious a lot, can Ayurveda help?" | |
- "My digestion has been off" | |
- "I want to start eating healthier" | |
""") | |
# Event handlers | |
msg_input.submit(chat_interface, [msg_input, chatbot], [msg_input, chatbot]) | |
send_btn.click(chat_interface, [msg_input, chatbot], [msg_input, chatbot]) | |
clear_btn.click(lambda: [], outputs=[chatbot]) | |
test_btn.click(test_llm_connections, outputs=[status_display]) | |
save_btn.click( | |
save_profile, | |
[profile_name, profile_age, profile_constitution, profile_concerns], | |
[profile_status] | |
) | |
gr.HTML(""" | |
<div style="text-align: center; margin-top: 20px; padding: 15px; | |
background: #000000; border-radius: 10px;"> | |
<p><strong>π This is a truly conversational AI!</strong> It understands context, asks follow-up questions, and has natural conversations about your health.</p> | |
<p><strong>Disclaimer:</strong> Dr. Ayur provides educational information only. Always consult qualified practitioners for medical concerns.</p> | |
</div> | |
""") | |
if __name__ == "__main__": | |
app.launch(share=True) |