Spaces:
Sleeping
Sleeping
| import os | |
| import json | |
| import itertools | |
| from groq import Groq | |
| from dotenv import load_dotenv | |
| from pymongo import MongoClient | |
| import certifi | |
| import random | |
| load_dotenv() | |
| def get_random(): | |
| lst = ["R1","R2","R3","S1","RD1"] | |
| i = random.randint(0,4) | |
| return lst[i] | |
| some = get_random() | |
| GROQ_API_KEY = os.getenv(f"GROQ_API_KEY{some}") | |
| # GROQ_API_KEY = os.getenv("GROQ_API_KEY") | |
| def get_mongo_collection(): | |
| CONNECTION_STRING = os.getenv("CONNECTION_STRING") | |
| DB_NAME = os.getenv("DB_NAME") | |
| COLLECTION_NAME = os.getenv("COLLECTION_NAME") | |
| try: | |
| # Connect with certifi to avoid SSL errors | |
| client = MongoClient(CONNECTION_STRING, tlsCAFile=certifi.where()) | |
| db = client[DB_NAME] | |
| return db[COLLECTION_NAME] | |
| except Exception as e: | |
| print(f"Error connecting to Mongo: {e}") | |
| return None | |
| def get_file_data(filepath): | |
| if(filepath.endswith(".json")): | |
| with open(filepath,"r") as f: | |
| data = json.load(f) | |
| return data | |
| else: | |
| with open(filepath,"r") as f: | |
| data = f.read() | |
| return data | |
| def groq_calls(prompt): | |
| client = Groq(api_key=GROQ_API_KEY) | |
| groq_card = client.chat.completions.create( | |
| # 401629 | |
| messages=[ | |
| { | |
| "role": "user", | |
| "content": f"{prompt}", | |
| }, | |
| { | |
| "role":"system", | |
| "content":"You are a psychologist. Output JSON" | |
| } | |
| ], | |
| model="openai/gpt-oss-20b", | |
| response_format={"type": "json_object"} | |
| ) | |
| # print(groq_card.choices[0].message.content) | |
| return groq_card.choices[0].message.content | |
| def write_to_file(output_filename,groq_response): | |
| # output_filename = "overall_specific_problempair.json" | |
| parsed_json = json.loads(groq_response) | |
| with open(output_filename, 'a', encoding='utf-8') as f: | |
| json.dump(parsed_json, f) | |