climateeeeeeee3 / app.py
anekcb's picture
Update app.py
c8ef30c verified
import openai
import gradio as gr
# Initialize an empty conversation history list
conversation_history = []
def chatbot_with_submit(user_input, api_token):
global conversation_history
# If this is the first user input, provide the chatbot introduction message
if not conversation_history:
conversation_history.append({"content": "You are an environmental chatbot named EcoGuide. Please provide information and advice on climate-related topics only. This includes questions related to climate change, environmental issues, sustainable practices, renewable energy, and biodiversity conservation. Please avoid responding to non-environmental topics or questions that are not related to climate or environmental concerns."})
# Add the user input to the conversation history
conversation_history.append({"content": user_input})
# Set the OpenAI API key
openai.api_key = api_token
# Use OpenAI's API to generate a response
prompt = "\n".join([message['content'] for message in conversation_history])
response = openai.Completion.create(
engine="gpt-3.5-turbo",
prompt=prompt,
temperature=0.7,
max_tokens=1024,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
# Add the chatbot's response to the conversation history
chatbot_reply = response.choices[0].text.strip()
conversation_history.append({"content": chatbot_reply})
# Return the chatbot's response
return chatbot_reply
my_input = gr.inputs.Textbox(lines=2, label='Ask ClimateApp any questions about climate-related applications!', placeholder="Type your question here...")
my_output = gr.outputs.Textbox(label="ECOWATCH AI's Response")
api_input = gr.inputs.Textbox(label="Enter your OpenAI API token:", placeholder="Insert your API token here...")
demo = gr.Interface(
fn=chatbot_with_submit,
inputs=[my_input, api_input],
outputs=my_output,
title="ECOWATCH AI -GPT",
description='''Disclaimer: Please consult climate experts for critical matters. Our forecasts may occasionally be inaccurate.'''
)
# Share the link
demo.launch(share=False)