Spaces:
Running
Running
File size: 19,742 Bytes
f6d265b 5cd2da7 3c86359 5cd2da7 f6d265b 3c86359 5cd2da7 f6d265b 5cd2da7 f6d265b 5cd2da7 f6d265b 5cd2da7 f6d265b 8326975 3c86359 5cd2da7 f6d265b 5cd2da7 8326975 5cd2da7 3c86359 5cd2da7 8326975 5cd2da7 8326975 5cd2da7 8326975 5cd2da7 3c86359 5cd2da7 3c86359 5cd2da7 3c86359 5cd2da7 3c86359 5cd2da7 3c86359 5cd2da7 3c86359 5cd2da7 3c86359 f6d265b 5cd2da7 f6d265b 5cd2da7 3c86359 5cd2da7 3c86359 5cd2da7 3c86359 5cd2da7 3c86359 5cd2da7 3c86359 5cd2da7 3c86359 5cd2da7 3c86359 5cd2da7 3c86359 5cd2da7 3c86359 5cd2da7 3c86359 5cd2da7 3c86359 5cd2da7 3c86359 5cd2da7 3c86359 5cd2da7 3c86359 5cd2da7 3c86359 5cd2da7 3c86359 8326975 3c86359 5cd2da7 3c86359 8326975 3c86359 5cd2da7 f6d265b 5cd2da7 3c86359 5cd2da7 3c86359 f6d265b 5cd2da7 3c86359 5cd2da7 3c86359 5cd2da7 8326975 3c86359 5cd2da7 3c86359 8326975 5cd2da7 8326975 5cd2da7 3c86359 5cd2da7 3c86359 8326975 5cd2da7 3c86359 6dbf5e2 5cd2da7 3c86359 5cd2da7 |
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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 |
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) |