File size: 6,316 Bytes
c7a6fe6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
def inference_prompt_revise_summary(fulltext, ref_summary, generated_summary, version, missing_subclaims):
    prompt = f"""
You are a medical summarization model specialized in readability-controlled text revision.

Your task is to improve the **Generated Summary** by adding back the key missing clinical information listed under **Missing Subclaims**, while keeping the readability style defined for the level **{version}**.

Do not copy the reference summary. Keep coherence, brevity, and correctness.

---

### INPUT

**Full Text (for context):**
{fulltext}

**Reference Summary (for comparison only):**
{ref_summary}

**Generated Summary (to revise):**
{generated_summary}

**Missing Subclaims (to integrate naturally):**
{missing_subclaims}

---

### READABILITY STYLES

- **easy (FH 70–100, grade 5–7):**
  - Short sentences, familiar vocabulary, concrete ideas.
  - Avoid subordinate clauses and medical jargon.
  - Tone: explanatory, simple, and friendly.

- **intermediate (FH 50–69, grade 8–12):**
  - Moderate sentence complexity and domain vocabulary.
  - Clear and structured explanation.

- **hard (FH 0–49, university/professional):**
  - Use specialized terminology, formal and dense phrasing.
  - Include:
    - precise domain vocabulary;
    - causal or analytical connectors (por consiguiente, sin embargo, dado que…);
    - one definition, one process description, and one implication statement if possible;
    - optional subordinate clauses for academic rhythm.

---

### OUTPUT
Return the result in the following JSON format:

{{
  "revised_summary": "<your revised summary text here>"
}}

Ensure the text is coherent, medically accurate, and matches the **{version}** readability level.
"""
    return prompt


from openai import OpenAI
import json
file_path = "/home/mshahidul/api_new.json"
with open(file_path, "r") as file:
    api_keys = json.load(file)

openai_api_key = api_keys.get("openai")

client = OpenAI(api_key=openai_api_key)
def openai_return(prompt):
    response = client.chat.completions.create(
        model="gpt-5",
        messages=[
            {"role": "system", "content": "You are a helpful assistant."},
            {"role": "user", "content": prompt}
        ]
    )
    try:
        cleaned_response = response.choices[0].message.content.strip().replace("```json", "").replace("```", "")
        return json.loads(cleaned_response)
    except Exception as e:
        return response.choices[0].message.content.strip().replace("```json", "").replace("```", "")
import json
file_path = "/home/mshahidul/readctrl/data/training_data_subclaim_verifier/synthetic_data_es_subclaims_100.json"

with open(file_path, 'r') as f:
    synthetic_data = json.load(f)



with open("/home/mshahidul/readctrl/results/dataset_quality_check/completeness_resonability_check_100_qwen3-32B_v3.json", 'r') as f:
    readability_reasoning = json.load(f)

import json, ast

reason_info = {}

for item in readability_reasoning:
    id = item['id']
    difficulty_level = item['version']
    data_temp = item['completeness']
    for _data in data_temp['results']:
        reasonableness = _data['reasonableness']
        
        # Step 1: Try to parse as JSON
        if isinstance(reasonableness, str):
            parsed = None
            try:
                parsed = json.loads(reasonableness)
            except Exception:
                try:
                    parsed = ast.literal_eval(reasonableness)
                except Exception:
                    # Not JSON or dict — treat as plain text
                    parsed = {"reasonableness": "unknown", "justification": reasonableness}
            reasonableness = parsed

        # Step 2: Skip if "reasonable"
        if reasonableness.get('reasonableness') in ["reasonable","unknown"]:
            continue

        # Step 3: Collect non-reasonable subclaims
        key = (id, difficulty_level)
        reason_info.setdefault(key, []).append(_data['subclaim'])



file_path_qwen3_32B = "/home/mshahidul/readctrl/results/dataset_quality_check/subclaim_verifier_results_100_qwen3-32B.json"

with open(file_path_qwen3_32B, 'r') as f:
    qwen3_32B_results = json.load(f)

# def inference_prompt_revise_summary(fulltext, ref_summary, generated_summary, version, missing_subclaims):
import os
with open("/home/mshahidul/readctrl/data/testing_data_gs/multiclinsum_gs_train_es.json", "r") as f_train:
    multiclinsum_gs_train_es = json.load(f_train)
dat_full_text={}
dat_summary={}
for item in multiclinsum_gs_train_es:
    dat_full_text[item['id']]=item['fulltext']
    dat_summary[item['id']]=item['summary']
res=[]
save_path = "/home/mshahidul/readctrl/results/dataset_quality_check/results_revised_100_gpt5_v3.json"
if os.path.exists(save_path):
    with open(save_path, 'r') as f:
        res = json.load(f)
existing_check=set((entry['id'], entry['difficulty_level']) for entry in res)
print(f"Resuming from {len(res)} entries")
import tqdm
for ind in tqdm.tqdm(range(0,10)):
    for version in ["easy", "intermediate", "hard"]:
        reference_summary = (f"{synthetic_data[ind]['ref_summary']['text']}")
        generated_summary = (f"{synthetic_data[ind]['readability_versions'][version]['text']}")
        if (synthetic_data[ind]['id'],version) in existing_check:
            continue
        if (synthetic_data[ind]['id'],version) not in reason_info or len(reason_info[(synthetic_data[ind]['id'],version)])==0:
            continue
        missing_subclaims = reason_info[(synthetic_data[ind]['id'],version)]
        prompt = inference_prompt_revise_summary(dat_full_text[synthetic_data[ind]['id']], reference_summary, generated_summary, version, missing_subclaims)
        try:
            ans=openai_return(prompt)
            res.append({
                "id": synthetic_data[ind]['id'],
                "difficulty_level": version,
                "prompt": prompt,
                "response": ans
            })
            
            if len(res)%2==0:
                print(f"Completed {len(res)} out of 300")
                with open(save_path, 'w') as outfile:
                    json.dump(res, outfile, indent=2)
        except Exception as e:
            print(f"Error at index {ind}, version {version}: {e}")

with open(save_path, 'w') as outfile:
    json.dump(res, outfile, indent=2)