chatgpt-test / app.py
techdestech's picture
Create app.py
1dbcfbb verified
import os
import openai
import gradio as gr
openai.api_key ='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'
prompt = "MERHABALAR"
def openai_create(prompt):
response = openai.Completion.create(
model="text-davinci-003",
prompt=prompt,
temperature=0,
max_tokens=1500,
top_p=1.0,
frequency_penalty=0.0,
presence_penalty=0.0
)
return response.choices[0].text
SysIns = [{"role": "system", "content": "You are a professional stock analyst."}]
SysIns.append({"role": "system", "content": "Türkçe"})
messages = SysIns
def openai_chatcreate(prompt):
messages.append({"role":"user","content":prompt})
response = openai.ChatCompletion.create(
model='gpt-3.5-turbo',
messages=messages
)
messages.append(response.choices[0]["message"])
return response.choices[0]["message"]["content"]
def chatgpt_chatclone(input, history):
history = history or []
s = list(sum(history,()))
s.append(input)
output = openai_chatcreate(input)
history.append((input, output))
return history, history
def chatgpt_clear(input, history):
messages = []
messages = SysIns
return "", [], []
blocks = gr.Blocks()
with blocks:
chatbot = gr.Chatbot(label="Danışman")
message = gr.Textbox(placeholder=prompt, label="Cevaplarınızın uzunluğuna göre cevap gecikebilir.")
state = gr.State()
submit = gr.Button("Send")
submit.click(fn=chatgpt_chatclone, inputs=[message, state], outputs=[chatbot, state])
clear = gr.Button("Clear")
clear.click(fn=chatgpt_clear, inputs=[message, state], outputs=[message, chatbot, state])
blocks.launch(debug=False)