Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
BharatGen AyurParam β Swastik.fit AI Vaidya
|
| 3 |
+
Hosted on HuggingFace Spaces with ZeroGPU (free, no credit card needed)
|
| 4 |
+
|
| 5 |
+
Model: bharatgenai/AyurParam (2.9B params, trained on 1,000+ Ayurvedic texts)
|
| 6 |
+
License: CC-BY-4.0 (commercial OK)
|
| 7 |
+
Prompt format: <user> {question} <assistant>
|
| 8 |
+
|
| 9 |
+
This Space is called by the Swastik Cloud Function (ayurParamProxy).
|
| 10 |
+
The /run/predict endpoint receives: { data: ["<user> ... <assistant>"] }
|
| 11 |
+
Returns: { data: ["response text"] }
|
| 12 |
+
"""
|
| 13 |
+
|
| 14 |
+
import spaces
|
| 15 |
+
import gradio as gr
|
| 16 |
+
import torch
|
| 17 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 18 |
+
|
| 19 |
+
MODEL_ID = "bharatgenai/AyurParam"
|
| 20 |
+
|
| 21 |
+
tokenizer = None
|
| 22 |
+
model = None
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def load_model():
|
| 26 |
+
global tokenizer, model
|
| 27 |
+
if model is not None:
|
| 28 |
+
return
|
| 29 |
+
print("[AyurParam] Loading tokenizer...")
|
| 30 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=False)
|
| 31 |
+
print("[AyurParam] Loading model...")
|
| 32 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 33 |
+
MODEL_ID,
|
| 34 |
+
trust_remote_code=True,
|
| 35 |
+
torch_dtype=torch.bfloat16,
|
| 36 |
+
device_map="auto",
|
| 37 |
+
)
|
| 38 |
+
model.eval()
|
| 39 |
+
print("[AyurParam] Model ready.")
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
# Load on startup
|
| 43 |
+
load_model()
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
@spaces.GPU
|
| 47 |
+
def generate(prompt: str) -> str:
|
| 48 |
+
"""
|
| 49 |
+
Main inference function.
|
| 50 |
+
Accepts either:
|
| 51 |
+
- Raw prompt already formatted: "<user> ... <assistant>"
|
| 52 |
+
- Plain text question (will be wrapped automatically)
|
| 53 |
+
Returns: assistant response only (no prompt echo)
|
| 54 |
+
"""
|
| 55 |
+
if not prompt or not prompt.strip():
|
| 56 |
+
return "Please provide a question."
|
| 57 |
+
|
| 58 |
+
# Ensure correct prompt format
|
| 59 |
+
if "<user>" not in prompt:
|
| 60 |
+
formatted = f"<user> {prompt.strip()} <assistant>"
|
| 61 |
+
else:
|
| 62 |
+
# Already formatted β ensure it ends with <assistant>
|
| 63 |
+
formatted = prompt.strip()
|
| 64 |
+
if not formatted.endswith("<assistant>"):
|
| 65 |
+
formatted = formatted + " <assistant>"
|
| 66 |
+
|
| 67 |
+
inputs = tokenizer(formatted, return_tensors="pt").to(model.device)
|
| 68 |
+
input_len = inputs["input_ids"].shape[1]
|
| 69 |
+
|
| 70 |
+
with torch.no_grad():
|
| 71 |
+
output = model.generate(
|
| 72 |
+
**inputs,
|
| 73 |
+
max_new_tokens=512,
|
| 74 |
+
do_sample=True,
|
| 75 |
+
top_k=50,
|
| 76 |
+
top_p=0.95,
|
| 77 |
+
temperature=0.6,
|
| 78 |
+
eos_token_id=tokenizer.eos_token_id,
|
| 79 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 80 |
+
use_cache=True,
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
# Decode only the new tokens (not the prompt)
|
| 84 |
+
new_tokens = output[0][input_len:]
|
| 85 |
+
response = tokenizer.decode(new_tokens, skip_special_tokens=True).strip()
|
| 86 |
+
|
| 87 |
+
# Clean up any trailing special tokens
|
| 88 |
+
for stop in ["<user>", "<context>", "</s>"]:
|
| 89 |
+
if stop in response:
|
| 90 |
+
response = response[: response.index(stop)].strip()
|
| 91 |
+
|
| 92 |
+
return response
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
# Gradio interface β Swastik Cloud Function calls /run/predict directly
|
| 96 |
+
demo = gr.Interface(
|
| 97 |
+
fn=generate,
|
| 98 |
+
inputs=gr.Textbox(
|
| 99 |
+
label="Prompt",
|
| 100 |
+
placeholder="<user> What foods should I eat for better digestion? <assistant>",
|
| 101 |
+
lines=3,
|
| 102 |
+
),
|
| 103 |
+
outputs=gr.Textbox(label="AyurParam Response", lines=8),
|
| 104 |
+
title="BharatGen AyurParam β Ayurveda AI",
|
| 105 |
+
description=(
|
| 106 |
+
"**AyurParam** is India's first AI trained on 1,000+ Ayurvedic texts (54.5M words). "
|
| 107 |
+
"2.9B parameter model fine-tuned on classical Ayurveda knowledge.\n\n"
|
| 108 |
+
"Prompt format: `<user> your question <assistant>`\n\n"
|
| 109 |
+
"This Space powers the AI Vaidya at [swastik.fit](https://swastik.fit)."
|
| 110 |
+
),
|
| 111 |
+
examples=[
|
| 112 |
+
["<user> What foods should I eat to improve digestion according to Ayurveda? <assistant>"],
|
| 113 |
+
["<user> I have vata imbalance β what daily routine do you recommend? <assistant>"],
|
| 114 |
+
["<user> What are the benefits of turmeric in Ayurvedic medicine? <assistant>"],
|
| 115 |
+
["<user> namaste <assistant>"], # warmup ping
|
| 116 |
+
],
|
| 117 |
+
cache_examples=False,
|
| 118 |
+
api_name="predict", # enables /run/predict endpoint
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
if __name__ == "__main__":
|
| 122 |
+
demo.launch()
|