gradio_chatbot / app.py
hpratapsingh's picture
Update app.py
6fc6c90 verified
import gradio as gr
import os
import openai
# Set your OpenAI API key
openai.api_key = os.environ.get("OAPI")
title = "OpenAI Agriculture Chatbot Demo"
description = "Example of a simple chatbot with Gradio using OpenAI's GPT-3.5 model"
placeholder = "Ask me anything..."
examples = ["What crops are suitable for the upcoming monsoon season?",
"How can I improve soil fertility in my farmland?",
"Could you provide tips for pest management in organic farming?"]
def chat_with_openai(user_input, history):
# Use OpenAI's completion endpoint to generate a response
completion = openai.Completion.create(
engine="gpt-3.5-turbo-instruct", # Choose the GPT-3.5 model engine
prompt=user_input,
max_tokens=100 # Adjust based on the desired response length
)
return completion.choices[0].text.strip()
iface = gr.ChatInterface(
fn=chat_with_openai,
chatbot=gr.Chatbot(height=300),
textbox=gr.Textbox(placeholder=placeholder, container=False, scale=7),
title=title,
description=description,
theme="light", # Adjust theme as needed
examples=examples,
cache_examples=True,
retry_btn=None,
undo_btn="Undo",
clear_btn="Clear",
)
iface.launch(share=True)