import os import re import gradio as gr from huggingface_hub import InferenceClient # ---------------------------------------- # HF Setup (use HF Router) # ---------------------------------------- MODEL_ID = "Psychotherapy-LLM/PsychoCounsel-Llama3-8B" HF_TOKEN = os.getenv("HF_TOKEN") if HF_TOKEN: client = InferenceClient(model=MODEL_ID, token=HF_TOKEN) else: client = InferenceClient(model=MODEL_ID) # ---------------------------------------- # Build prompt # ---------------------------------------- def build_prompt(client_speech: str, context: str, mode: str) -> str: if mode == "Brief (5–7 sentences)": instruction = ( "You are a professional psychotherapist. Respond in 5–7 warm, reflective, " "empathic sentences. Only output what the therapist says." ) else: instruction = ( "You are a professional psychotherapist. Produce a detailed multi-paragraph " "therapeutic response similar to the Appendix case studies. Start with validation, " "explore beliefs, discuss values, suggest a small exercise, and end with an open question." ) if context: instruction += f" Therapist context: {context}" return f"""{instruction} Client Speech: {client_speech} Therapist: """ # ---------------------------------------- # Generate # ---------------------------------------- def generate_response(client_speech, context, mode, temperature, top_p): if not client_speech or not client_speech.strip(): return "⚠️ Please enter client speech." prompt = build_prompt(client_speech, context, mode) max_tokens = 220 if mode == "Brief (5–7 sentences)" else 450 try: output = client.text_generation( prompt, max_new_tokens=max_tokens, temperature=float(temperature), top_p=float(top_p), do_sample=True, return_full_text=False, ) except Exception as e: return f"⚠️ HF API Error: {e}" text = output.strip() text = text.split("Note:")[0].split("FINAL ANSWER")[0].strip() # Limit to 7 sentences in brief mode if mode == "Brief (5–7 sentences)": sents = re.split(r"(?<=[.!?])\s+", text) text = " ".join(sents[:7]) return text # ---------------------------------------- # UI # ---------------------------------------- DEFAULT_CLIENT_SPEECH = ( "I’ve been having emotional issues for a few years. Nonetheless, these have been somewhat " "manageable. However, I became increasingly paranoid this winter..." ) DESCRIPTION = ( "This replicates Appendix-style psychotherapist responses using PsychoCounsel-Llama3-8B.\n\n" "⚠️ Not for clinical use. Research demo only." ) with gr.Blocks(title="PsychoCounsel-Llama3-8B — Research Demo") as demo: gr.Markdown("# 🧠 PsychoCounsel-Llama3-8B — Original / Research Demo") gr.Markdown(DESCRIPTION) with gr.Row(): with gr.Column(scale=1): mode_radio = gr.Radio( ["Brief (5–7 sentences)", "Extended (Appendix-style)"], value="Extended (Appendix-style)", label="Response Style" ) temp = gr.Slider(0.1, 1.0, value=0.6, step=0.05, label="Temperature") top_p = gr.Slider(0.5, 1.0, value=0.9, step=0.05, label="Top-p") with gr.Column(scale=2): speech_box = gr.Textbox( label="Client Speech", value=DEFAULT_CLIENT_SPEECH, lines=12 ) context_box = gr.Textbox( label="Optional Therapist Context", value="", lines=3 ) generate_btn = gr.Button("Generate Therapist Response", variant="primary") output = gr.Markdown() generate_btn.click( generate_response, inputs=[speech_box, context_box, mode_radio, temp, top_p], outputs=output, ) if __name__ == "__main__": demo.launch()