File size: 2,152 Bytes
ca36b93
efb0883
ed59c5c
651dc33
 
 
efb0883
651dc33
ca36b93
651dc33
 
efb0883
651dc33
 
4e59524
651dc33
efb0883
 
 
651dc33
452cfcc
651dc33
ad85bb9
452cfcc
651dc33
 
 
 
 
ca36b93
651dc33
 
452cfcc
 
651dc33
 
452cfcc
ca36b93
c8e833a
 
d963676
efb0883
1c0665f
c8ef30c
1c0665f
efb0883
1c0665f
c8e833a
9d3a878
1c0665f
ca36b93
9d3a878
c8ef30c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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)