import gradio as gr import spaces pipe = None def get_text(content): if isinstance(content, str): return content if isinstance(content, list): parts = [] for p in content: if isinstance(p, dict): parts.append(p.get("text", "")) else: parts.append(str(p)) return "".join(parts) if isinstance(content, dict): return content.get("text", str(content)) return str(content) @spaces.GPU def respond(message, history): global pipe try: if pipe is None: import torch from transformers import pipeline pipe = pipeline( "text-generation", model="Qwen/Qwen2.5-0.5B-Instruct", dtype=torch.bfloat16, device_map="auto", ) messages = [{"role": "system", "content": "You are BrainBox, a helpful friendly assistant."}] for h in history: if isinstance(h, dict): messages.append({"role": h["role"], "content": get_text(h["content"])}) elif isinstance(h, (list, tuple)) and len(h) >= 2: messages.append({"role": "user", "content": get_text(h[0])}) messages.append({"role": "assistant", "content": get_text(h[1])}) messages.append({"role": "user", "content": get_text(message)}) out = pipe(messages, max_new_tokens=256, do_sample=True, temperature=0.7) gen = out[0]["generated_text"] if isinstance(gen, list): return get_text(gen[-1].get("content", "")) if isinstance(gen[-1], dict) else str(gen[-1]) return str(gen) except Exception as e: return f"Error: {e}" demo = gr.ChatInterface(fn=respond, title="BrainBox Chat") demo.launch()