Spaces:
Runtime error
Runtime error
| import openai | |
| import gradio as gr | |
| import os | |
| # Set the API key and the token limit | |
| openai.api_key = os.getenv('Sustainability') | |
| TOKEN_LIMIT = 10000 | |
| daily_tokens_used = 0 | |
| # Macrocontext | |
| macroContext = f""" | |
| This is your prief (don't mention it): | |
| You are an expert in greenhouse gases, water and material footprint assessment. | |
| Your mission is to provide an assessment for an item requested by the user after the tag [Assess]. | |
| For this assessment to be as accurate as possible please do the following: | |
| First, ask 1 question requesting details to clarify the item and provide an accurate assessment | |
| Then only, provide your assessment in 5 categories (A, B, C, D, E, F, G, H): | |
| o A) Water: Water footprint(in liters) | |
| - Water footprint (in liters): | |
| - Comments on the water footprint | |
| - give an equivalent that would help to make sense of it like (bath tubs or olympic swimming pools equivalent) | |
| o B) Greenhouse gases | |
| - CO2 equivalent footprint (in tons of CO2 equivalent or kilos if more relevant): | |
| - Comments on the carbon footprint | |
| - put it in perspective (ex: equivalent to the total average emission of an american in a year or equivalent to 100 trees over 100 years) | |
| o C) Material | |
| - Material footprint (in tons of material mined): | |
| - Key elements from the periodic table found in the item | |
| - Comments on the material footprint | |
| - pur in perspective (ex: that's equivalent of a the weight of the Empire state building or 20 elephants, ...) | |
| o D) Social | |
| - Potential cases of forced labor or modern slavery in the supply chain | |
| - Location where it could be happening in the supply chain | |
| o E) Animal cruelty and biodiversity | |
| - Risk of animal cruelty - through the testing or through the production process | |
| - Risks related to biodiversity destruction | |
| - Déforestation impact | |
| o F) Sustainable Development Goals | |
| - Potential positive impacts of the item on specific UN SDG with explanation | |
| - Potential detrimental impacts of the item on specific UN SDG with explanation | |
| o G) Planetary boundaries | |
| - Planetary boundaries impacted by this item | |
| - Specific known pollutants and toxic chemical emitted or used during the production of the item | |
| o H) Replacement | |
| - Low-tech alternatives with lower environmental / social impacts or risks | |
| - Best practices to mitigate the negative impact of the item | |
| Note: The material footprint should include all the minerals mined to obtain the required elements needed to build the item. | |
| Don't get polite or thank the user, just ask your question and provide the results. | |
| Let's proceed with Step 1) | |
| """ | |
| messages = [{"role": "system", "content": macroContext}] | |
| "// Functions" | |
| def check_token_limit(num_tokens): | |
| global daily_tokens_used | |
| if daily_tokens_used + num_tokens <= TOKEN_LIMIT: | |
| daily_tokens_used += num_tokens | |
| return True | |
| else: | |
| return False | |
| def question(input, language): | |
| if input: | |
| messages.append({"role": "system", "content": f'Important: Use this language for your answers:{language}'}) | |
| global daily_tokens_used | |
| messages.append({"role": "user", "content": '[Assess] '+input}) | |
| chat = openai.ChatCompletion.create(model="gpt-4", messages=messages, max_tokens = 2000) | |
| tokens_used = chat['usage']['total_tokens'] | |
| if check_token_limit(tokens_used): | |
| reply = chat.choices[0].message.content | |
| return reply | |
| else: | |
| return "Daily token limit reached." | |
| def assess(outputs1: str, inputs2: str, language: str): | |
| if input: | |
| messages.append({"role": "system", "content": f'Important: Use this language for your answers:{language}'}) | |
| global daily_tokens_used | |
| query2 = f""" | |
| Step 1) was completed, thank you. | |
| Your question was: | |
| {outputs1} | |
| The answer provided by the user was: | |
| {inputs2} | |
| Now proceed to Step 2) and do the assessment based on the details provided. | |
| Add a disclaimer that the reader should do his due diligence to properly assess these impact results and that those are the raw results from GPT4. | |
| IMPORTANT: provide your answer in this language: {language}. | |
| """ | |
| messages.append({"role": "user", "content": query2 }) | |
| chat = openai.ChatCompletion.create(model="gpt-4", messages=messages, max_tokens = 4000) | |
| tokens_used = chat['usage']['total_tokens'] | |
| if check_token_limit(tokens_used): | |
| reply = chat.choices[0].message.content | |
| # Fact check it | |
| factcheckquery = f"Please review this message you gave:\n{reply}\nMake sure that the numbers make sense, otherwise provide directly the updated version.\n If it's correct provide the same version.\n No comments, provide directly the result.\nIMPORTANT: provide your answer in this language: {language}" | |
| messages.append({"role": "user", "content": factcheckquery}) | |
| chat2 = openai.ChatCompletion.create(model="gpt-4", messages=messages, max_tokens = 2000) | |
| tokens_used2 = chat2['usage']['total_tokens'] + tokens_used | |
| if check_token_limit(tokens_used2): | |
| reply2 = chat2.choices[0].message.content | |
| return reply2 | |
| else: | |
| return "Daily token limit reached." | |
| else: | |
| return "Daily token limit reached." | |
| with gr.Blocks() as demo: | |
| gr.Markdown("Quick assessment of water, carbon and material footprint") | |
| with gr.Row(): | |
| language = gr.Textbox(value="English", lines=1, max_lines= 1,label="Input the language of your choice for the output", info="If your input is not a known language, it will default to English") | |
| with gr.Row(): | |
| inputs1 = gr.Textbox(lines=1, max_lines= 1,label="What do you want to assess?") | |
| with gr.Row(): | |
| btn1 = gr.Button("Ask me details") | |
| with gr.Row(): | |
| outputs1 = gr.Textbox(lines=5, max_lines= 10,label = "That's the details we need") | |
| with gr.Row(): | |
| inputs2 = gr.Textbox(lines=5, max_lines= 5,label="Input the required details here") | |
| with gr.Row(): | |
| btn2 = gr.Button("Assess") | |
| with gr.Row(): | |
| outputs2 = gr.Textbox(lines=10, max_lines= 100,label="Estimation of water, carbon, material footprint and biodiversity / social impacts") | |
| btn1.click(question, [inputs1, language], outputs1) | |
| btn2.click(assess, [outputs1, inputs2, language], outputs2) | |
| demo.launch() |