File size: 5,803 Bytes
2c61137
 
 
6a5956a
2c61137
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a5956a
2c61137
 
 
 
 
 
 
 
 
 
 
 
 
 
0b1b2db
2c61137
 
 
 
 
 
 
 
 
 
 
 
e3ae0f8
2c61137
 
e3ae0f8
2c61137
 
 
 
 
e3ae0f8
6a5956a
2c61137
 
6a5956a
 
 
e3ae0f8
 
2c61137
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e3ae0f8
 
 
2c61137
 
e3ae0f8
2c61137
 
 
e3ae0f8
2c61137
 
 
 
 
 
 
 
 
 
6a5956a
2c61137
6a5956a
2c61137
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e3ae0f8
2c61137
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import gradio as gr
import openai
import pytesseract
import random
import os
import time

Init_system_prompt = "You are an AI Assistant that tries to teach kids various subjects. You are given learning material and you task is to ask questions given the material and then you also grade answers and give feedback how to improve the answers"
system_message = {"role": "system", "content": Init_system_prompt}

import os
from PIL import Image

import pytesseract
#os.system("rm -f path.txt")
path = os.system("which tesseract >> path.txt")
with open("path.txt", 'r') as file:
    tesseract_path = file.read().replace('\n', '')


########### TAB 1 (UPLOAD) FUNCTIONS  #############################

def print_files(files):
    for file in files:
        print(file.__dir__())
        print(file.name)
        print(file.file)


def create_data(files):
    question_context = ''
    for file in files:
        if file.name.endswith('png') or file.name.endswith('.jpg'):
            try:
                question_context += (pytesseract.image_to_string(Image.open(file.name))) + '\n\n'
            except Exception as e:
                print(e)
                pass

    return question_context


########### TAB 3 (CHAT) FUNCTIONS  #############################

def user(user_message, history):
        return "", history + [[user_message, None]]

def bot(history, messages_history, api_key, system_prompt, teksti_contexti, temperature, max_tokens, chatgpt_model, max_context_size_for_question):
    user_message = history[-1][0]

    bot_message, messages_history = ask_gpt(user_message, messages_history, api_key, system_prompt, teksti_contexti, temperature, max_tokens, chatgpt_model, max_context_size_for_question)
    messages_history += [{"role": "assistant", "content": bot_message}]
    history[-1][1] = bot_message
    time.sleep(0.2)
    return history, messages_history, str(messages_history)

def ask_gpt(message, messages_history, api_key, system_prompt, context, temperature, max_tokens, chatgpt_model, max_context_size_for_question):
    messages_history, system_prompt, _ = init_history(messages_history, system_prompt)
    if len(messages_history) < 1:
        messages_history = [{"role": "system", "content": system_prompt}]
    max_possible_position = len(context)- max_context_size_for_question
    start = random.randint(0,max_possible_position)
    messages_history += [{"role": "user", "content": context[start:start+max_context_size_for_question] + '\n Please ask a question about the previous paragramph?'}]
    
    openai.api_key = api_key
    response = openai.ChatCompletion.create(
        model=chatgpt_model,
        messages=messages_history,
        temperature=temperature,
        max_tokens=max_tokens
    )
    return response['choices'][0]['message']['content'], messages_history

def init_history(messages_history, system_prompt):
    messages_history = []
    messages_history += [{"role": "system", "content": system_prompt}]
    msg_log = gr.Textbox.update(value="Tähän tulee message history")
    system_prompt = gr.Textbox.update(value=system_prompt, label='Insert system message here')
    return messages_history, system_prompt, msg_log


############# INTERFACE ##########################
with gr.Blocks() as demo:
    gr.Markdown("ChatGPT demo with RAG using Chromadb")
    
    
    
    ############# TAB 1 ##########################
    with gr.Tab("Upload documents and create context"):
        with gr.Row():
            api_key = gr.Textbox(value='', type='password', label='Insert OPENAI API-key here')
        with gr.Row():
            files = gr.File(file_count='multiple', file_types=['image'], interactivate = True)
            create_context_btn = gr.Button(value='Recognize text and create context')
        with gr.Row():
            gr.Markdown("")
        with gr.Row():
            teksti_contexti = gr.Textbox(value='Tähän tulee konteksti', label='Created context')
    
    ############# TAB 3 ##########################

    with gr.Tab("Chat"):
        gr.Markdown("""<h1><center>ChatGPT 
        ChatBot with Gradio and OpenAI</center></h1>
        """)
        with gr.Row():
            system_prompt = gr.Textbox(value=Init_system_prompt, label='Insert system message here')
            chatgpt_model = gr.Dropdown(choices=["gpt-3.5-turbo", "gpt-3.5-turbo-0301", "gpt-3.5-turbo-0613"], value='gpt-3.5-turbo',label='ChatGPT model to use', interactive=True)
            temperature = gr.Slider(minimum=0.0, maximum=1.0, step=0.05, value=0.7, label='temperature')
            max_tokens = gr.Slider(minimum=10, maximum=600, step=10, value=100, label='Max tokens')
            max_context_size_for_question = gr.Slider(minimum=10, maximum=600, step=10, value=100, label='Max context for questions')
        with gr.Row():
            chatbot = gr.Chatbot(label='ChatGPT Chat')
            state = gr.State([])
        with gr.Row():
            msg = gr.Textbox()
        with gr.Row():
            clear = gr.Button("Clear")
        with gr.Row():
            msg_log = gr.Textbox("Tähän tulee message history", label='Message history')
                    
        
    with gr.Accordion("Klikkaa avataksesi ohjeet"):
        gr.Markdown("Ohjeet tulee tänne")


    # TAB 1 (UPLOAD) Interactive elements:
    create_context_btn.click(create_data, files, teksti_contexti)


    # TAB 3 (CHAT) Interactive elements:
    msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
        bot, [chatbot, state, api_key, system_prompt, teksti_contexti, temperature, max_tokens, chatgpt_model, max_context_size_for_question], [chatbot, state, msg_log]
    )
    clear.click(lambda: None, None, chatbot, queue=False).success(init_history, [state, system_prompt], [state, system_prompt, msg_log])


demo.launch(debug=True)