|
import gradio as gr |
|
import openai |
|
from osbot_utils.utils.Dev import pprint |
|
|
|
from test_bot.api.Open_API import Open_API |
|
|
|
Open_API().setup() |
|
|
|
|
|
|
|
class Gradio_Test: |
|
|
|
|
|
def __init__(self): |
|
pass |
|
|
|
def title(self): |
|
return "# Chat GPT Powered demo" |
|
|
|
def default_prompt(self): |
|
system_prompt = """You are an AI-powered CISO (Chief Information Security Officer) bot, designed to provide guidance and answer questions related to cybersecurity and information security. You have extensive knowledge in securing systems, protecting data, implementing best practices, and addressing security concerns. Users will seek your assistance for advice, information, and solutions on a wide range of security topics. Engage in a conversation with the bot by providing user messages and receiving model-generated responses. |
|
|
|
User: Hi, I have some security concerns and questions. Can you help me? |
|
|
|
CISO Bot:""" |
|
return {"role": "system", "content": system_prompt} |
|
|
|
def predict(self, message, history): |
|
history_openai_format = [] |
|
history_openai_format.append(self.default_prompt()) |
|
for human, assistant in history: |
|
history_openai_format.append({"role": "user", "content": human}) |
|
history_openai_format.append({"role": "assistant", "content": assistant}) |
|
history_openai_format.append({"role": "user", "content": message}) |
|
|
|
pprint(history_openai_format) |
|
response = openai.ChatCompletion.create( |
|
model='gpt-3.5-turbo', |
|
messages=history_openai_format, |
|
temperature=1.0, |
|
stream=True |
|
) |
|
|
|
partial_message = "" |
|
for chunk in response: |
|
if len(chunk['choices'][0]['delta']) != 0: |
|
partial_message = partial_message + chunk['choices'][0]['delta']['content'] |
|
yield partial_message |
|
|
|
def create_demo(self): |
|
|
|
|
|
|
|
|
|
|
|
|
|
with gr.Blocks() as demo: |
|
gr.Markdown(self.title()) |
|
textbox_input = gr.Textbox(value='Hello, good morning' , render=False) |
|
gr.ChatInterface(self.predict, textbox=textbox_input) |
|
|
|
demo.queue() |
|
return demo |
|
|
|
|
|
def create_demo__2(self): |
|
title = self.title() |
|
|
|
with gr.Blocks() as demo: |
|
gr.Markdown(title) |
|
inp = gr.Textbox(placeholder="What is your name?") |
|
out = gr.Textbox() |
|
|
|
inp.change(fn=lambda x: f"Welcome .... , {x}!", |
|
inputs=inp, |
|
outputs=out) |
|
|
|
return demo |