Compare commits
19 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| a29d9fcba5 | |||
| c986ab92c5 | |||
| b2e9ccd2b6 | |||
| 2f1bd2bfd0 | |||
| c145b66cdf | |||
| 0da8440496 | |||
| cc830f00e8 | |||
| ce3baff6cc | |||
| a1a8abfb8e | |||
| 8f34f06578 | |||
| eabde3fcb1 | |||
| 2a715233ee | |||
| a415632552 | |||
| 16aa6c206e | |||
| c11a81548a | |||
| e453cf379c | |||
| 454273a6cb | |||
| 2cab5fd9b3 | |||
| 90436584f8 |
21
.gitignore
vendored
21
.gitignore
vendored
@@ -1,7 +1,16 @@
|
||||
# Ignore all contents of these directories
|
||||
!**/*.py
|
||||
/Data/
|
||||
/attach/
|
||||
/results/
|
||||
/enarcelona/
|
||||
# 1. Broad Ignores
|
||||
/Data/*
|
||||
/attach/*
|
||||
/results/*
|
||||
/enarcelona/*
|
||||
.env
|
||||
__pycache__/
|
||||
*.pyc
|
||||
/reference/
|
||||
# 2. Ignore virtual environments COMPLETELY
|
||||
# This must come BEFORE the unignore rule
|
||||
env*/
|
||||
|
||||
# 3. The "Unignore" rule (Whitelisting)
|
||||
# We only unignore .py files that aren't already blocked by the rules above
|
||||
!**/*.py
|
||||
|
||||
1296
Data/show_plots.py
Normal file
1296
Data/show_plots.py
Normal file
File diff suppressed because it is too large
Load Diff
5
app.py
5
app.py
@@ -214,3 +214,8 @@ if __name__ == "__main__":
|
||||
print(f"Results saved to {output_json}")
|
||||
##
|
||||
|
||||
|
||||
|
||||
# %% name
|
||||
eXXXXXXXX
|
||||
##
|
||||
|
||||
149
total_app.py
Normal file
149
total_app.py
Normal file
@@ -0,0 +1,149 @@
|
||||
import time
|
||||
import json
|
||||
import os
|
||||
from datetime import datetime
|
||||
import pandas as pd
|
||||
from openai import OpenAI
|
||||
from dotenv import load_dotenv
|
||||
|
||||
# Load environment variables
|
||||
load_dotenv()
|
||||
|
||||
# === CONFIGURATION ===
|
||||
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
||||
OPENAI_BASE_URL = os.getenv("OPENAI_BASE_URL")
|
||||
MODEL_NAME = "GPT-OSS-120B"
|
||||
HEALTH_URL = f"{OPENAI_BASE_URL}/health" # Placeholder - actual health check would need to be implemented
|
||||
CHAT_URL = f"{OPENAI_BASE_URL}/chat/completions"
|
||||
# File paths
|
||||
INPUT_CSV = "/home/shahin/Lab/Doktorarbeit/Barcelona/Data/MS_Briefe_400_with_unique_id_SHA3_explore_cleaned_unique.csv"
|
||||
EDSS_INSTRUCTIONS_PATH = "/home/shahin/Lab/Doktorarbeit/Barcelona/attach/Komplett.txt"
|
||||
#GRAMMAR_FILE = "/home/shahin/Lab/Doktorarbeit/Barcelona/attach/just_edss_schema.gbnf"
|
||||
# Initialize OpenAI client
|
||||
client = OpenAI(
|
||||
api_key=OPENAI_API_KEY,
|
||||
base_url=OPENAI_BASE_URL
|
||||
)
|
||||
# Read EDSS instructions from file
|
||||
with open(EDSS_INSTRUCTIONS_PATH, 'r') as f:
|
||||
EDSS_INSTRUCTIONS = f.read().strip()
|
||||
|
||||
# === RUN INFERENCE 2 ===
|
||||
def run_inference(patient_text, max_retries=3):
|
||||
prompt = f'''Du bist ein medizinischer Assistent, der spezialisiert darauf ist, EDSS-Scores (Expanded Disability Status Scale) sowie alle Unterkategorien aus klinischen Berichten zu extrahieren.
|
||||
### Regeln für die Ausgabe:
|
||||
1. **Reason**: Erstelle eine prägnante Zusammenfassung (max. 400 Zeichen) der Befunde auf **DEUTSCH**, die zur Einstufung führen.
|
||||
2. **klassifizierbar**:
|
||||
- Setze dies auf **true**, wenn ein EDSS-Wert identifiziert, berechnet oder basierend auf den klinischen Hinweisen plausibel geschätzt werden kann.
|
||||
- Setze dies auf **false**, NUR wenn die Daten absolut unzureichend oder so widersprüchlich sind, dass keinerlei Einstufung möglich ist.
|
||||
3. **EDSS**:
|
||||
- Dieses Feld ist **VERPFLICHTEND**, wenn "klassifizierbar" auf true steht.
|
||||
- Es muss eine Zahl zwischen 0.0 und 10.0 sein.
|
||||
- Versuche stets, den EDSS-Wert so präzise wie möglich zu bestimmen, auch wenn die Datenlage dünn ist (nutze verfügbare Informationen zu Gehstrecke und Funktionssystemen).
|
||||
- Dieses Feld **DARF NICHT ERSCHEINEN**, wenn "klassifizierbar" auf false steht.
|
||||
4. **Unterkategorien**:
|
||||
- Extrahiere alle folgenden Unterkategorien aus dem Bericht:
|
||||
- VISUAL OPTIC FUNCTIONS (max. 6.0)
|
||||
- BRAINSTEM FUNCTIONS (max. 6.0)
|
||||
- PYRAMIDAL FUNCTIONS (max. 6.0)
|
||||
- CEREBELLAR FUNCTIONS (max. 6.0)
|
||||
- SENSORY FUNCTIONS (max. 6.0)
|
||||
- BOWEL AND BLADDER FUNCTIONS (max. 6.0)
|
||||
- CEREBRAL FUNCTIONS (max. 6.0)
|
||||
- AMBULATION (max. 10.0)
|
||||
- Jede Unterkategorie sollte eine Zahl zwischen 0.0 und der jeweiligen Obergrenze enthalten, wenn sie klassifizierbar ist
|
||||
- Wenn eine Unterkategorie nicht klassifizierbar ist, setze den Wert auf null
|
||||
### Einschränkungen:
|
||||
- Erfinde keine Fakten, aber nutze klinische Herleitungen aus dem Bericht, um den EDSS und die Unterkategorien zu bestimmen.
|
||||
- Priorisiere die Vergabe eines EDSS-Wertes gegenüber der Markierung als nicht klassifizierbar.
|
||||
- Halte dich strikt an die JSON-Struktur.
|
||||
- Die Unterkategorien müssen immer enthalten sein, auch wenn sie null sind.
|
||||
EDSS-Bewertungsrichtlinien:
|
||||
{EDSS_INSTRUCTIONS}
|
||||
Patientenbericht:
|
||||
{patient_text}
|
||||
'''
|
||||
|
||||
start_time = time.time()
|
||||
for attempt in range(max_retries + 1):
|
||||
try:
|
||||
response = client.chat.completions.create(
|
||||
messages=[
|
||||
{
|
||||
"role": "system",
|
||||
"content": "You extract EDSS scores and all subcategories. You prioritize providing values even if data is partial, by using clinical inference."
|
||||
},
|
||||
{
|
||||
"role": "user",
|
||||
"content": prompt
|
||||
}
|
||||
],
|
||||
model=MODEL_NAME,
|
||||
max_tokens=2048,
|
||||
temperature=0.0,
|
||||
response_format={"type": "json_object"}
|
||||
)
|
||||
content = response.choices[0].message.content
|
||||
|
||||
if content is None or content.strip() == "":
|
||||
raise ValueError("API returned empty or None response content")
|
||||
|
||||
parsed = json.loads(content)
|
||||
inference_time = time.time() - start_time
|
||||
return {
|
||||
"success": True,
|
||||
"result": parsed,
|
||||
"inference_time_sec": inference_time
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
print(f"Attempt {attempt + 1} failed: {e}")
|
||||
if attempt < max_retries:
|
||||
time.sleep(2 ** attempt) # Exponential backoff
|
||||
continue
|
||||
else:
|
||||
print("All retries exhausted.")
|
||||
return {
|
||||
"success": False,
|
||||
"error": str(e),
|
||||
"inference_time_sec": -1
|
||||
}
|
||||
|
||||
# === BUILD PATIENT TEXT ===
|
||||
def build_patient_text(row):
|
||||
# Handle potential NaN or None values in the row
|
||||
summary = str(row.get("T_Zusammenfassung", "")) if pd.notna(row.get("T_Zusammenfassung")) else ""
|
||||
diagnoses = str(row.get("Diagnosen", "")) if pd.notna(row.get("Diagnosen")) else ""
|
||||
clinical = str(row.get("T_KlinBef", "")) if pd.notna(row.get("T_KlinBef")) else ""
|
||||
findings = str(row.get("T_Befunde", "")) if pd.notna(row.get("T_Befunde")) else ""
|
||||
return "\n".join([summary, diagnoses, clinical, findings]).strip()
|
||||
if __name__ == "__main__":
|
||||
# Read CSV file ONLY inside main block
|
||||
df = pd.read_csv(INPUT_CSV, sep=';')
|
||||
results = []
|
||||
# Process each row
|
||||
for idx, row in df.iterrows():
|
||||
print(f"Processing row {idx + 1}/{len(df)}")
|
||||
try:
|
||||
patient_text = build_patient_text(row)
|
||||
result = run_inference(patient_text)
|
||||
# Add unique_id and MedDatum to result for tracking
|
||||
result["unique_id"] = row.get("unique_id", f"row_{idx}")
|
||||
result["MedDatum"] = row.get("MedDatum", None)
|
||||
results.append(result)
|
||||
print(json.dumps(result, indent=2, ensure_ascii=False))
|
||||
except Exception as e:
|
||||
print(f"Error processing row {idx}: {e}")
|
||||
results.append({
|
||||
"success": False,
|
||||
"error": str(e),
|
||||
"unique_id": row.get("unique_id", f"row_{idx}"),
|
||||
"MedDatum": row.get("MedDatum", None)
|
||||
})
|
||||
# Save results to a JSON file
|
||||
output_json = INPUT_CSV.replace(".csv", "_results_total.json")
|
||||
with open(output_json, 'w', encoding='utf-8') as f:
|
||||
json.dump(results, f, indent=2, ensure_ascii=False)
|
||||
print(f"Results saved to {output_json}")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user