SwissAI-Team7 / summarize.py
Muzansama's picture
Upload 3 files
2ab9254 verified
raw
history blame
4.01 kB
import os
from urllib import response
import requests
from tqdm import tqdm
from openai import OpenAI
parent = '/capstor/scratch/cscs/dshah/AI/CleanedHospitalData'
openrouterapi = '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'
# 'sk-2defa7c8af874651a32f6ce5c4eb2675'
def get_from_or(file_path):
if file_path.endswith('.json'):
file_path = os.path.join(parent, file_path)
else:
return 'error'
output_file = file_path.replace('CleanedHospitalData', 'SummarizedCleanedHospitalData')
if os.path.exists(output_file):
return 'error'
with open(file_path, 'r') as file:
content = file.read()
client = OpenAI(
base_url="https://openrouter.ai/api/v1",
api_key=openrouterapi,
)
completion = client.chat.completions.create(
# model="deepseek/deepseek-chat-v3.1:free",
model="x-ai/grok-4-fast:free",
# messages= [
# {
# 'role': 'user',
# 'content': content,
# },
# {'role': 'user', 'content': 'Summarize the above text with Hospital name as the heading and in key word format. Make it as precise as possible and do not assume anything apart from the text. Give me the keywords related to the technologies that are offered by the hospital, The specialties they have and the services they provide. Ignore writing everything else. Do not write any warnings or disclaimers, only useful text.' }
# ],
# )
messages= [
{
'role': 'user',
'content': [{'type': 'text', 'text': content}],
},
{'role': 'user', 'content': [{'type': 'text', 'text': 'Summarize the above text with Hospital name as the heading and in key word format. Make it as precise as possible and do not assume anything apart from the text. Give me the keywords related to the technologies that are offered by the hospital, The specialties they have and the services they provide. Ignore writing everything else. Do not write any warnings or disclaimers, only useful text.'}] }
],
)
try:
output = (completion.choices[0].message.content)
with open(output_file, 'w') as out_file:
out_file.write(output)
return output
except Exception as e:
print(f"Error occurred: {e}")
return 'error'
headers = {
'Authorization': 'Bearer ' + 'yae32JuX8lZGeTEWKSMqHjcNlTa0',
'Content-Type': 'application/json',
}
def get_json_for_file(file_path):
with open(file_path, 'r') as file:
content = file.read()
return {
'model': 'swiss-ai/Apertus-70B',
'messages': [
{
'role': 'user',
'content': content,
},
{'role': 'user', 'content': 'Summarize the above text in key word format. Make it as precise as possible and do not assume anything apart from the text. Give me the keywords related to the technologies that are offered by the hospital, The specialties they have and the services they provide. Ignore writing everything else. Do not write any warnings or disclaimers, only useful text.' }
],
}
# json_data = {
# 'model': 'swiss-ai/Apertus-70B',
# 'messages': [
# {
# 'role': 'user',
# 'content': 'Hello, how are you?',
# },
# ],
# }
# for file in tqdm(os.listdir(parent)):
files = os.listdir(parent)
import multiprocessing as mp
if __name__ == '__main__':
with mp.Pool(processes=100) as pool:
pool.map(get_from_or, files)
# json_data = get_json_for_file(file_path)
# response = requests.post(
# 'https://api.swisscom.com/layer/swiss-ai-weeks/apertus-70b/v1/chat/completions',
# headers=headers,
# json=json_data,
# )
# with open(output_file, 'w') as out_file:
# out_file.write(response.text)
# response_text = get_from_or(file_path)
# print(response.json())