Spaces:
No application file
No application file
| """ | |
| Data Scientist.: Dr.Eddy Giusepe Chirinos Isidro | |
| Neste script realizamos novamente um resumo de um documento | |
| de maneira mais automatizada. | |
| """ | |
| import openai | |
| import json | |
| from termcolor import colored | |
| import logging | |
| logging.basicConfig(level=logging.INFO) | |
| #Substitua sua chave de API OpenAI: | |
| import openai | |
| import os | |
| from dotenv import load_dotenv, find_dotenv | |
| _ = load_dotenv(find_dotenv()) # read local .env file | |
| openai.api_key = os.environ['OPENAI_API_KEY'] | |
| model = "gpt-3.5-turbo-16k-0613" # model = "gpt-4-0613" | |
| def summarize_text(text, model): | |
| response = openai.ChatCompletion.create( | |
| model = model, | |
| messages = [ | |
| {"role": "user", "content": f"Resumir: {text}"}, | |
| ], | |
| stream = True, | |
| functions = [ | |
| { | |
| "name": "resumir_documento", | |
| "description": "Resume um documento retornando um resumo", | |
| "parameters": { | |
| "type": "object", | |
| "properties": { | |
| "summary": { | |
| "type": "string", | |
| "description": "um breve resumo do documento." | |
| } | |
| }, | |
| "required": ["summary"] | |
| } | |
| } | |
| ], | |
| function_call = {"name": "resumir_documento"} # Pode ser "auto" or "none" | |
| ) | |
| responses = '' | |
| for chunk in response: | |
| # print(chunk) | |
| if chunk["choices"][0]["delta"].get("function_call"): | |
| chunk = chunk["choices"][0]["delta"] | |
| # print(chunk) | |
| summary = chunk["function_call"]["arguments"] | |
| responses += summary | |
| print(summary, end='', flush=True) # Aqui mostrará no terminal os Tokens em streaming | |
| print("") | |
| summary = json.loads(responses)["summary"] | |
| logging.info("*** 🤗 Imprimindo o RESUMO 🤗 ***") | |
| return summary | |
| # read the contents of sample.txt | |
| with open('sample.txt', 'r', encoding="utf-8", errors="ignore") as file: | |
| sample = file.read().replace('\n', '') | |
| print(colored(summarize_text(sample, model), "red")) |