Skip to content
Python

Add files via upload

QI CHENbda0f694ce9a
diff --git a/src/generate_distortion.py b/src/generate_distortion.py
new file mode 100644
index 0000000..1f3f740
--- /dev/null
+++ b/src/generate_distortion.py
@@ -0,0 +1,189 @@
+import pandas as pd
+import requests
+import json
+import time
+import random
+import os
+from typing import List, Dict
+
+
+class CBTAnalyzer:
+
+ def __init__(self, api_key: str = "YOUR_API_KEY", model: str = "deepseek-chat"):
+ self.api_key = api_key
+ self.model = model
+ self.base_url = "https://api.deepseek.com/v1"
+
+ def load_samples(self, excel_path: str) -> List[Dict]:
+ df = pd.read_excel(excel_path)
+ samples = []
+
+ for _, row in df.iterrows():
+ sample = {
+ "problem": row['Thought/Statement'],
+ "cognitive_distortions": row['cognitive_distortions']
+ }
+ samples.append(sample)
+
+ return samples
+
+ def build_system_prompt(self, samples: List[Dict]) -> str:
+
+ few_shot_examples = ""
+ for i, sample in enumerate(random.sample(samples, min(10, len(samples))), 1):
+ few_shot_examples += f"{i}. Question: {sample['problem']}\n Cognitive Distortions: {sample['cognitive_distortions']}\n\n"
+ system_prompt = f"""You are a CBT psychologist.And analyze cognitive distortions and output in ENGLISH ONLY.
+
+Cognitive Distortion Types:
+1. All-or-nothing thinking
+2. Overgeneralization
+3. Mental filter
+4. Discounting the positives
+5. Mind reading
+6. Fortune telling
+7. Magnification or minimization
+8. Emotional reasoning
+9. Should statements
+10. Labeling
+11. Personalization and blame
+
+Examples:
+{few_shot_examples}
+Use exact JSON format below:
+
+{{
+ "question": "English translation here",
+ "distortions": ["Distortion"]
+}}
+Please follow the sample dataset.
+If no distortions found, use empty list: "distortions": []"""
+
+ return system_prompt
+
+ def analyze_batch(self, questions: List[str], samples_path: str,
+ batch_size: int = 20, delay: float = 1) -> List[Dict]:
+
+ samples = self.load_samples(samples_path)
+
+ results = []
+ no_distortion_count = 0
+ total_batches = (len(questions) + batch_size - 1) // batch_size
+
+ for i in range(0, len(questions), batch_size):
+ batch = questions[i:i + batch_size]
+ batch_num = i // batch_size + 1
+
+ system_prompt = self.build_system_prompt(samples)
+
+ batch_distortion_count = 0
+
+ print(f"Processing batch {batch_num}/{total_batches}, {len(batch)} questions in this batch")
+
+ for question in batch:
+ try:
+ result = self._analyze_single(question, system_prompt)
+ if result.get("distortions") and len(result["distortions"]) > 0:
+ results.append(result)
+ batch_distortion_count += len(result["distortions"])
+ else:
+ no_distortion_count += 1
+ time.sleep(delay)
+ except Exception as e:
+ print(f"Analysis failed: {question[:50]}... Error: {e}")
+
+ print(f"Batch {batch_num} identified {batch_distortion_count} cognitive distortions")
+
+ if no_distortion_count > 0:
+ print(f"Total {no_distortion_count} questions with no cognitive distortions")
+
+ return results
+
+ def _analyze_single(self, question: str, system_prompt: str) -> Dict:
+ headers = {
+ "Authorization": f"Bearer {self.api_key}",
+ "Content-Type": "application/json"
+ }
+
+ user_content = f"Analyze this question: {question}"
+
+ data = {
+ "model": self.model,
+ "messages": [
+ {"role": "system", "content": system_prompt},
+ {"role": "user", "content": user_content}
+ ],
+ "temperature": 0.1,
+ "response_format": {"type": "json_object"}
+ }
+
+ response = requests.post(
+ f"{self.base_url}/chat/completions",
+ headers=headers,
+ json=data
+ )
+
+ if response.status_code == 200:
+ result = json.loads(response.json()['choices'][0]['message']['content'])
+ return {
+ "question": result.get("question", ""),
+ "distortions": result.get("distortions", [])
+ }
+ else:
+ raise Exception(f"API call failed: {response.status_code} - {response.text}")
+
+
+def main():
+ current_dir = os.path.dirname(os.path.abspath(__file__))
+ analyzer = CBTAnalyzer(api_key="your_api_key")
+ psyqa_path = os.path.join(current_dir, "questions.xlsx")
+ psyqa_df = pd.read_excel(psyqa_path)
+ questions = psyqa_df['question'].tolist()
+
+ print(f"Loaded {len(questions)} questions to analyze")
+
+ samples_path = os.path.join(current_dir, "CBT_Cognitive_Triplet_Dataset.xlsx")
+ results = analyzer.analyze_batch(
+ questions=questions,
+ samples_path=samples_path,
+ batch_size=15,
+ delay=1.5
+ )
+
+ print(f"Successfully analyzed {len(results)} questions with cognitive distortions")
+ if len(results) == 0:
+ print("No questions with cognitive distortions identified")
+ return pd.DataFrame()
+
+ results_df = pd.DataFrame(results)
+
+ results_df['distortion'] = results_df['distortions'].apply(
+ lambda x: x[0] if isinstance(x, list) and len(x) > 0 else 'No distortion'
+ )
+
+ final_df = pd.DataFrame({
+ 'question': results_df['question'],
+ 'distortion': results_df['distortion']
+ })
+ output_path = os.path.join(current_dir, "distortion.xlsx")
+
+ final_df.to_excel(output_path, index=False, engine='openpyxl')
+ print(f"Analysis complete! Results saved to: {output_path}")
+ print(f"Exported {len(final_df)} questions with cognitive distortions")
+
+ distortion_freq = final_df['distortion'].value_counts()
+ print("\n" + "=" * 50)
+ print("CBT Analysis Statistics Report")
+ print("=" * 50)
+ print(f"Total questions: {len(questions)}")
+ print(f"Questions with cognitive distortions: {len(final_df)} ({len(final_df) / len(questions) * 100:.1f}%)")
+ print(f"Questions without cognitive distortions: {len(questions) - len(final_df)} ({(len(questions) - len(final_df)) / len(questions) * 100:.1f}%)")
+ print(f"Cognitive distortion type distribution:")
+
+ for distortion, count in distortion_freq.items():
+ percentage = (count / len(final_df)) * 100
+ print(f" {distortion}: {count} ({percentage:.1f}%)")
+ return final_df
+
+
+if __name__ == "__main__":
+ final_results = main()
\ No newline at end of file
diff --git a/src/generate_response.py b/src/generate_response.py
new file mode 100644
index 0000000..7c9981c
--- /dev/null
+++ b/src/generate_response.py
@@ -0,0 +1,207 @@
+import pandas as pd
+import json
+import time
+import random
+from typing import List, Dict
+from openai import OpenAI
+import os
+
+class CBTProfessionalResponder:
+ def __init__(self, model: str = "gpt-5-mini", random_seed: int = None):
+ self.model = model
+ self.random_seed = random_seed
+
+ self.client = OpenAI(
+ api_key=os.environ.get("OPENAI_API_KEY"),
+ base_url="https://api.openai.com/v1"
+ )
+ self.samples = None
+
+ def load_samples(self, excel_path: str, sample_size: int = 10, random_seed: int = None) -> List[Dict]:
+ df = pd.read_excel(excel_path)
+
+ if len(df) < sample_size:
+ raise ValueError(f"Insufficient data: requested {sample_size} samples, but data file only has {len(df)} rows")
+
+ if random_seed is not None:
+ random.seed(random_seed)
+ elif self.random_seed is not None:
+ random.seed(self.random_seed)
+
+ indices = random.sample(range(len(df)), sample_size)
+ samples = []
+
+ for i in indices:
+ row = df.iloc[i]
+ sample = {
+ "question": row['question'] if 'question' in df.columns else row.get('Thought/Statement', ''),
+ "distortion": row['distortion'] if 'distortion' in df.columns else row.get('cognitive_distortions', ''),
+ "response": row.get('rational_response', row.get('response', ''))
+ }
+ samples.append(sample)
+
+ self.samples = samples
+ print(f"Randomly loaded {len(samples)} sample examples")
+ print(f"Randomly selected sample indices: {sorted(indices)}")
+
+ return samples
+
+ def build_prompt(self, question: str, distortion: str) -> str:
+
+ examples_text = ""
+ if self.samples:
+ examples_text = "Reference examples:\n"
+ for i, sample in enumerate(self.samples, 1):
+ examples_text += f"\nExample {i}:\n"
+ examples_text += f"Question: {sample['question']}\n"
+ examples_text += f"Cognitive Distortion: {sample['distortion']}\n"
+ examples_text += f"Professional Response: {sample['response']}\n"
+
+ prompt = f"""You are a cognitive behavioral therapy (CBT) psychologist. Based on the patient's type of cognitive distortion and specific situation, please provide a professional and compassionate response. Your primary goal is to establish a safe atmosphere of trust and understanding, ensuring your reply includes the following CBT components, appropriately segmented, and connecting each part in an organized and fluid manner.
+
+{examples_text}
+
+1. Validation and Empathy: Acknowledge and express understanding and sympathy for the user's emotional experience and the issues raised. Respond to their emotions with warm, empathetic language, like a close friend, to build trust and a sense of security.
+
+2. Identifying Cognitive Distortions: Briefly explain, using both professional and everyday language, how this thinking pattern might be affecting the user, based on the types of cognitive distortions provided in the Excel sheet and the specific situation.
+
+3. Proposing Gentle Cognitive Challenges: Use open-ended reflective questions to gently and non-confrontationally help the user reconsider this thinking pattern.
+
+4. Providing CBT Strategies: Offer practical CBT techniques directly targeting the identified cognitive distortions, including both professional terminology and detailed, easy-to-understand explanations.
+
+5. Encouragement and Closing Remarks: Encourage the user and remind them that changes in cognition and emotions are a gradual and ongoing process.
+
+Input Format:
+question:{question}
+distortion:{distortion}
+
+Output Format (JSON):
+{{
+ "question": "original question",
+ "distortion": "cognitive distortion type",
+ "cbt_response": "your integrated CBT response paragraph"
+}}
+"""
+
+ return prompt
+
+ def generate_response(self, question: str, distortion: str) -> Dict:
+
+ prompt = self.build_prompt(question, distortion)
+
+ try:
+ response = self.client.chat.completions.create(
+ model=self.model,
+ messages=[
+ {"role": "system", "content": "You are a cognitive behavioral therapy (CBT) psychologist."},
+ {"role": "user", "content": prompt}
+ ],
+ temperature=0.3,
+ max_tokens=800
+ )
+
+ response_content = response.choices[0].message.content
+
+ try:
+ parsed_response = json.loads(response_content)
+ return parsed_response
+ except json.JSONDecodeError:
+ print(f"JSON parsing failed, returning raw content: {response_content[:100]}...")
+ return {
+ "question": question,
+ "distortion": distortion,
+ "cbt_response": response_content
+ }
+
+ except Exception as e:
+ print(f"Error generating response: {e}")
+ return {
+ "question": question,
+ "distortion": distortion,
+ "cbt_response": f"Generation failed: {str(e)}"
+ }
+
+ def process_data_file(self,
+ sample_file: str,
+ data_file: str,
+ output_file: str,
+ delay: float = 1.0,
+ sample_size: int = 10) -> pd.DataFrame:
+
+ print("=" * 60)
+ print("Starting processing workflow")
+ print("=" * 60)
+
+ print(f"Loading data from {data_file}...")
+ data_df = pd.read_excel(data_file)
+ print(f"Loaded {len(data_df)} records to process")
+
+ results = []
+ total = len(data_df)
+
+ for idx, row in data_df.iterrows():
+ try:
+ question = row['question']
+ distortion = row['distortion']
+
+ print(f"Processing [{idx + 1}/{total}]: {question[:50]}...")
+
+ print(f"Randomly loading {sample_size} sample examples from {sample_file}...")
+ self.load_samples(sample_file, sample_size=sample_size)
+
+ response = self.generate_response(question, distortion)
+ cbt_response = response.get('cbt_response',
+ response.get('CBT_response',
+ str(response)))
+
+ result = {
+ "question": question,
+ "distortion": distortion,
+ "cbt_response": cbt_response,
+ }
+ results.append(result)
+
+ time.sleep(delay)
+
+ except Exception as e:
+ print(f"Error processing record {idx + 1}: {e}")
+ results.append({
+ "question": str(row.get('question', '')),
+ "distortion": str(row.get('distortion', '')),
+ "cbt_response": f"Processing failed: {str(e)}",
+ })
+
+ results_df = pd.DataFrame(results)
+ results_df.to_excel(output_file, index=False, engine='openpyxl')
+
+ print(f"\nProcessing complete! Results saved to: {output_file}")
+
+ return results_df
+
+def main():
+
+ responder = CBTProfessionalResponder(model="gpt-5-mini", random_seed=42)
+
+ current_dir = os.path.dirname(os.path.abspath(__file__))
+ sample_file = os.path.join(current_dir, "CBT_Cognitive_Triplet_Dataset.xlsx")
+ data_file = os.path.join(current_dir, "distortion.xlsx")
+ output_file = os.path.join(current_dir, "response.xlsx")
+
+ results = responder.process_data_file(
+ sample_file=sample_file,
+ data_file=data_file,
+ output_file=output_file,
+ delay=1.5,
+ sample_size=10
+ )
+
+ return results
+
+if __name__ == "__main__":
+ print("Cognitive Behavioral Therapy (CBT) Professional Response Generator")
+ print("=" * 60)
+
+ results = main()
+
+ if results is not None:
+ print("\n✅ Processing complete!")
\ No newline at end of file
diff --git a/src/model_test.py b/src/model_test.py
new file mode 100644
index 0000000..d6da53b
--- /dev/null
+++ b/src/model_test.py
@@ -0,0 +1,68 @@
+import pandas as pd
+from openai import OpenAI
+import time
+import os
+
+api_key = os.getenv("BEARER_TOKEN")
+
+INPUT_FILE = "test.xlsx"
+OUTPUT_FILE = "test_output.xlsx"
+STATEMENT_COLUMN = "user1"
+
+client = OpenAI(
+ api_key=api_key,
+ base_url="https://api.siliconflow.cn/v1"
+)
+
+FINE_TUNED_MODEL_ID = "ft:LoRA/Qwen/Qwen2.5-7B-Instruct:d50jhbk50mis73di8n5g:gpt5_mini:udjarjexxlodpjueztat-ckpt_step_625"
+
+
+def analyze_batch():
+ df = pd.read_excel(INPUT_FILE)
+
+ if 'assistant1' not in df.columns:
+ df['assistant1'] = ""
+
+ for i in range(len(df)):
+ if df.at[i, 'assistant1']:
+ continue
+
+ user_input = str(df.at[i, STATEMENT_COLUMN])
+ print(f"Processing {i + 1}/{len(df)}")
+
+ try:
+ response = client.chat.completions.create(
+ model=FINE_TUNED_MODEL_ID,
+ messages=[
+ {
+ "role": "system",
+ "content": """You are a cognitive behavioral therapy (CBT) psychologist. First, identify the type of cognitive distortion exhibited in the statement, and then provide a response containing the following five paragraphs, separated by blank lines:
+ 1.Empathy and Validation
+ 2.Cognitive Distortion Analysis
+ 3.Reflective Questions
+ 4.CBT Exercise Recommendation
+ 5.Encouragement and Next Steps.
+ If it does not contain a cognitive distortion (e.g., casual conversation, general questions, or statements without distortions), switch to a natural, supportive conversation mode. Respond in a warm, counselor-like tone without analyzing distortions or following the five-paragraph structure.
+"""
+ },
+ {
+ "role": "user",
+ "content": user_input
+ }
+ ],
+ temperature=0.2,
+ max_tokens=500
+ )
+
+ df.at[i, 'assistant1'] = response.choices[0].message.content
+ time.sleep(1)
+
+ except Exception as e:
+ df.at[i, 'assistant1'] = f"Error: {str(e)}"
+
+ df.to_excel(OUTPUT_FILE, index=False)
+ print(f"Completed! Results saved to: {OUTPUT_FILE}")
+
+
+if __name__ == "__main__":
+ analyze_batch()
\ No newline at end of file
diff --git a/src/model_test_demo.py b/src/model_test_demo.py
new file mode 100644
index 0000000..b55e61b
--- /dev/null
+++ b/src/model_test_demo.py
@@ -0,0 +1,37 @@
+from openai import OpenAI
+import os
+api_key = os.getenv("YOUR_API_KEY")
+
+client = OpenAI(
+ api_key=api_key,
+ base_url="https://api.siliconflow.cn/v1"
+)
+
+FINE_TUNED_MODEL_ID = "ft:LoRA/Qwen/Qwen2.5-7B-Instruct:d50jhbk50mis73di8n5g:gpt5_mini:udjarjexxlodpjueztat-ckpt_step_625"
+
+test_user_input = ("Is my career short-lived? Feeling lost about the future.")
+
+response = client.chat.completions.create(
+ model=FINE_TUNED_MODEL_ID,
+ messages=[
+ {
+ "role": "system",
+ "content": """You are a cognitive behavioral therapy (CBT) psychologist. First, identify the type of cognitive distortion exhibited in the statement, and then provide a response containing the following five paragraphs, separated by blank lines:1.Empathy and Validation
+ 2.Cognitive Distortion Analysis
+ 3.Reflective Questions
+ 4.CBT Exercise Recommendation
+ 5.Encouragement and Next Steps.
+ If it does not contain a cognitive distortion (e.g., casual conversation, general questions, or statements without distortions), switch to a natural, supportive conversation mode. Respond in a warm, counselor-like tone without analyzing distortions or following the five-paragraph structure.
+ """
+ },
+ {
+ "role": "user",
+ "content": test_user_input
+ }
+ ],
+ temperature=0.2,
+ max_tokens=300
+)
+
+print("\n--- Model Analysis Results ---\n")
+print(response.choices[0].message.content)
\ No newline at end of file