2

I'm attempting to create a chatbot app for a project, I currently have everything running with OpenAI and Gradio. This is what my code looks like right now:

import openai 
import gradio

openai.api_key = "api_key"

messages = [{"role": "system", "content": "You are a medical professional tasked in the at-home treatment of someone suffering from one of many chronic diseases"}]

def CustomChatGPT(user_input):
    messages.append({"role": "user", "content": user_input})
    response = openai.ChatCompletion.create(
        model = "gpt-3.5-turbo",
        messages = messages
    )
    ChatGPT_reply = response["choices"][0]["message"]["content"]
    messages.append({"role": "assistant", "content": ChatGPT_reply})
    return ChatGPT_reply

demo = gradio.Interface(fn=CustomChatGPT, inputs = "text", outputs = "text", title = "Oliver's Chronic Disease Chatbot")

demo.launch(share=True)

So far I can only access the chatbot as a link on my phone, but I would rather have it set up as an actual application I could download for a smartphone, specifically IOS. I've heard Flet is a good method in getting my code converted as an actual app, but I am not sure if its compatible with the OpenAI chatbot in my code. Has anyone tried this before, and if so, what is the feasibility of both Flet and and a GPT 3.5 Chatbot working together?

From the Flet website, I have this line of code pasted to my project:

import flet as ft

def main(page: ft.Page):
    page.add(ft.SafeArea(ft.Text("Hello, Flet!")))

ft.app(main)

Flet then instructs you to run this command into the terminal:

flet create <project-name>

When doing this, I get an error message that says:

+ flet create <03 chatgpt chat assistant website.py>
+             ~
The '<' operator is reserved for future use.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : RedirectionNotSupported

I'm a bit of a novice in Python, and especially in converting my code into actual frontend experiences, so I am not sure what this means. What is a possible solution for this? Is Flet not compatible with the OpenAI modules in my code? Would I have to remove the Gradio Modules of my code in order for Flet to run?

1 Answer 1

1

Firstly, the < and > should not be included in your app name. Also, it is better to not use any white spaces in your name when dealing with the command line interface. You should do something like:

flet create chatbot

Note that it is generally not safe to post your API key in a public forum, as other malicious individuals can use it and eat up your credit.

You can create a similar chatbot in flet.

# Inside chatbot.py
from openai import OpenAI
import flet as ft

client = OpenAI(
    api_key="YOUR_API_KEY",
)
messages = [{"role": "system", "content": "You are a medical professional tasked in the at-home treatment of someone suffering from one of many chronic diseases"}]

def CustomChatGPT(user_input):
    messages.append({"role": "user", "content": user_input})
    response = client.chat.completions.create(
        model="gpt-3.5-turbo",
        messages=messages
    )
    reply = response.choices[0].message.content
    messages.append({"role": "assistant", "content": reply})
    return reply

def main(page: ft.Page):
    page.title = "Oliver's Chronic Disease Chatbot"
    
    def send_message(e):
        user_input = input_box.value
        if user_input:
            output_box.value += f"You: {user_input}\n"
            response = CustomChatGPT(user_input)
            output_box.value += f"Bot: {response}\n"
            input_box.value = ""
            page.update()

    # UI
    input_box = ft.TextField(label="Your message", width=500)
    send_button = ft.ElevatedButton(text="Send", on_click=send_message)
    output_box = ft.TextField(value="", multiline=True, read_only=True, width=500, height=300)

    page.add(ft.Column([output_box, input_box, send_button]))

ft.app(target=main)

Remember to add your own API_KEY, you can test whether it works on your desktop by running it:

flet create chatbot.py
flet run chatbot

I am not familiar with flet but I am assuming it is similar to other mobile application frameworks. To run it on your phone, it is slightly more complicated, especially if you are using an iOS device. Assuming you are using Android, you need to bundle this app into an apk, and send the file to your phone.

You can look into the documentation.

Finally, note that putting API keys and other secrets on the client side is generally considered bad practice for security and practical reasons (what if you change your API key?).

A better alternative would be to use serverless functions for small applications like yours, such as Firebase's cloud functions.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the help! I removed my API key from the post, I wasn't paying attention and forgot to take it out beforehand. As for the terminal commands you suggested using, they still give me the same error message I highlighted on this post. This is when using the new suggested code and a new python file. Is there something I am doing wrong?
@ob01 can you try flet run chatbot.py?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.