import json import gradio as gr import spaces import torch from orbitals.claim_extractor import ClaimExtractor MODEL = "principled-intelligence/claim-extractor-4B-q-2605" # Loaded once at startup. On ZeroGPU, moving to "cuda" here is allowed; the # physical GPU is only attached while a @spaces.GPU function runs. ce = ClaimExtractor( backend="hf", model=MODEL, device="cuda", dtype=torch.bfloat16, # Library default is 20k, which could exhaust the per-call GPU window. max_new_tokens=2048, ) def _parse_conversation(raw: str): raw = raw.strip() if raw.startswith("[") or raw.startswith("{"): try: return json.loads(raw) except json.JSONDecodeError: pass return raw @spaces.GPU(duration=90) def extract(conversation: str, ai_service_description: str = "", intents_only: bool = False) -> dict: result = ce.extract( _parse_conversation(conversation), ai_service_description=ai_service_description.strip() or None, intents_only=intents_only, ) return result.model_dump(mode="json") demo = gr.Interface( fn=extract, inputs=[ gr.Textbox( label="Conversation", lines=10, placeholder='Plain text (one message), or JSON: [{"role": "user", "content": "..."}, ...]', ), gr.Textbox(label="AI service description (optional)", lines=4), gr.Checkbox(label="Intents only", value=False), ], outputs=gr.JSON(label="Extractions"), title="ClaimExtractor 4B", description="Extracts claims and intents from the last message of a conversation.", examples=[[ "Your package with tracking number 1234567890 is currently in transit and is expected " "to be delivered on December 12, 2025. If you want, I can also notify you when it is out for delivery.", "You are a virtual assistant for a parcel delivery service. You can only answer questions about package tracking.", False, ]], cache_examples=False, api_name="extract", ) if __name__ == "__main__": demo.queue(max_size=20).launch()