Coding-with-Adam / Dash-by-Plotly

Interactive data analytics
dash-by-plotly.coding-with-adam.vercel.app
1.46k stars 1.69k forks source link

Code error #58

Open nazarhktwitch opened 1 month ago

nazarhktwitch commented 1 month ago

Hi all, if you tried to run the code and you failed (like me for example) (Syntax error), I optimized the creator code in places where I was getting the error, here: (Read the information at the end)

import os
import openai  # pip install openai
from dash import Dash, dcc, html, Input, Output, State  # pip install dash
import dash_bootstrap_components as dbc  # pip install dash-bootstrap-components

openai.api_key = "YOUR_OPENAI_API_KEY"

model_options = [
    'text-davinci-003',
    'text-curie-001',
    'text-babbage-001',
    'text-ada-001',
    'text-davinci-002',
    'text-davinci-001'
]

app = Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])

app.layout = dbc.Container([
    html.H1("Dash-ChatGPT Example"),
    dbc.Row([
        dbc.Col([
            html.Label("Model:"),
            dcc.Dropdown(model_options, value="text-davinci-003", id='models'),
        ], width=6),
        dbc.Col([
            html.Label("Temperature:"),
            dcc.Slider(min=0, max=2, step=0.1, value=0.7, id='temperatures'),
        ], width=6)
    ], className='mb-5'),
    dbc.Row([
        dbc.Col([
            dcc.Input(id='input-text', type='text', placeholder='Type your message here', style={'width': 500}),
            html.Button('Submit', id='submit-button', n_clicks=0),
        ], width=6)
    ], justify="center"),
    dcc.Loading(
        children=[html.Div(id='output-text')],
        type="circle",
    )
])

@app.callback(
    Output('output-text', 'children'),
    Input('submit-button', 'n_clicks'),
    State('input-text', 'value'),
    State('models', 'value'),
    State('temperatures', 'value')
)
def update_output(n_clicks, text_input, model_input, temp_input):
    if n_clicks > 0:
        # Get the response from ChatGPT
        response = openai.Completion.create(
            model=model_input,
            prompt=text_input + "\n",
            max_tokens=400,
            temperature=temp_input
        )
        generated_text = response.choices[0].text
        # Return the generated text as the output
        return generated_text
    return ""

if __name__ == '__main__':
    app.run_server(debug=True)

Info: you MUST install the openai pip: pip install openai dash dash-bootstrap-components

Bye.

Coding-with-Adam commented 3 weeks ago

Thank you for your support @nazarhktwitch Do you mind sharing the link to the file of this code?

I would be happy to add your suggestions directly in the code.