Spaces:
Sleeping
Sleeping
ambrosfitz
commited on
Commit
•
71a2b31
1
Parent(s):
d247267
Update question_generator.py
Browse files- question_generator.py +42 -1
question_generator.py
CHANGED
@@ -23,7 +23,48 @@ model = "mistral-large-latest"
|
|
23 |
# Initialize Mistral client
|
24 |
client = MistralClient(api_key=api_key)
|
25 |
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
def generate_microbiology_question() -> Dict[str, str]:
|
29 |
"""Generate a microbiology question."""
|
|
|
23 |
# Initialize Mistral client
|
24 |
client = MistralClient(api_key=api_key)
|
25 |
|
26 |
+
def load_csv_data(file_path: str) -> List[Dict[str, str]]:
|
27 |
+
"""Load data from a CSV file."""
|
28 |
+
logging.info(f"Loading data from {file_path}...")
|
29 |
+
try:
|
30 |
+
with open(file_path, 'r', encoding='utf-8') as csvfile:
|
31 |
+
reader = csv.DictReader(csvfile)
|
32 |
+
data = list(reader)
|
33 |
+
logging.info(f"Loaded {len(data)} rows from {file_path}")
|
34 |
+
return data
|
35 |
+
except FileNotFoundError:
|
36 |
+
logging.error(f"File not found: {file_path}")
|
37 |
+
raise
|
38 |
+
except csv.Error as e:
|
39 |
+
logging.error(f"Error reading CSV file {file_path}: {e}")
|
40 |
+
raise
|
41 |
+
|
42 |
+
# Load data from both CSV files
|
43 |
+
try:
|
44 |
+
detailed_cases = load_csv_data('processed_medical_history.csv')
|
45 |
+
infectious_diseases = load_csv_data('infectious_diseases.csv')
|
46 |
+
except Exception as e:
|
47 |
+
logging.error(f"Failed to load CSV data: {e}")
|
48 |
+
raise
|
49 |
+
|
50 |
+
def hash_question(question: str) -> str:
|
51 |
+
"""Generate a hash for a question to check for duplicates."""
|
52 |
+
return hashlib.md5(question.encode()).hexdigest()
|
53 |
+
|
54 |
+
def load_generated_questions() -> set:
|
55 |
+
"""Load previously generated question hashes from a file."""
|
56 |
+
try:
|
57 |
+
with open('generated_questions.txt', 'r') as f:
|
58 |
+
return set(line.strip() for line in f)
|
59 |
+
except FileNotFoundError:
|
60 |
+
return set()
|
61 |
+
|
62 |
+
def save_generated_question(question_hash: str):
|
63 |
+
"""Save a newly generated question hash to the file."""
|
64 |
+
with open('generated_questions.txt', 'a') as f:
|
65 |
+
f.write(question_hash + '\n')
|
66 |
+
|
67 |
+
generated_questions = load_generated_questions()
|
68 |
|
69 |
def generate_microbiology_question() -> Dict[str, str]:
|
70 |
"""Generate a microbiology question."""
|