| |
| """UltimaBot.ipynb |
| |
| Automatically generated by Colaboratory. |
| |
| Original file is located at |
| https://colab.research.google.com/drive/1dnALgq0BKf3kxg3PSFoyc-9JNgix71iW |
| """ |
|
|
| |
| |
|
|
| import openai |
| import gradio as gr |
| import os |
|
|
| |
| openai.api_key = os.environ.get("API_OPENAI") |
|
|
| |
| |
| |
| |
| |
|
|
| model_engine = "text-davinci-003" |
| max_token_x = 1000 |
| temperature_x = 0.5 |
|
|
| def generate (input_text): |
| completion = openai.Completion.create( |
| engine=model_engine, |
| prompt=input_text, |
| top_p=1.0, |
| frequency_penalty=0.0, |
| presence_penalty=0.0, |
| max_tokens=max_token_x, |
| n=1, |
| stop=None, |
| temperature=temperature_x, |
| ) |
| response = completion.choices[0].text.strip() |
| return response |
|
|
| global_context = "" |
|
|
|
|
| def add_text(state, text): |
| global global_context |
| n_end=""" |
| """ |
| answer = generate(global_context + "Q:" + text + "A:" ) |
| global_context =global_context + n_end + "Q:" + text + n_end +"A:" + answer |
| state = state + [(text, answer)] |
| print("========global_context================") |
| print(global_context) |
| print("========================") |
| print("text", text) |
| print("answer", answer) |
| return state, state |
|
|
| def add_image(state, image): |
| state = state + [(f"", "Обнулили историю")] |
| prompt = "" |
| return state, state |
|
|
|
|
| with gr.Blocks(css="#chatbot .overflow-y-auto{height:300px} .gradio-container {background-color: lightblue}") as demo: |
| chatbot = gr.Chatbot(elem_id="chatbot") |
| state = gr.State([]) |
| |
| with gr.Row(): |
| with gr.Column(scale=0.85): |
| txt = gr.Textbox(show_label=False, placeholder="Enter text and press enter, or upload an image").style(container=False) |
| with gr.Column(scale=0.15, min_width=0): |
| btn = gr.UploadButton("🖼️", file_types=["image"]) |
| |
| txt.submit(add_text, [state, txt], [state, chatbot]) |
| txt.submit(lambda :"", None, txt) |
| btn.upload(add_image, [state, btn], [state, chatbot]) |
| greet_btn = gr.Button("Отправить") |
| greet_btn.click(add_text, [state, txt], [state, chatbot]) |
| |
| demo.launch(share=False, debug=False) |