Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code error #58

Closed
nazarhktwitch opened this issue May 26, 2024 · 2 comments
Closed

Code error #58

nazarhktwitch opened this issue May 26, 2024 · 2 comments

Comments

@nazarhktwitch
Copy link

nazarhktwitch commented May 26, 2024

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
Copy link
Owner

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.

@Coding-with-Adam
Copy link
Owner

Closing the issue since I can't find where this code is located.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants