Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,20 +1,10 @@
|
|
1 |
import gradio as gr
|
2 |
-
import pandas as pd
|
3 |
-
from io import StringIO
|
4 |
import openai
|
5 |
import pytesseract
|
6 |
import random
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
import os
|
11 |
-
|
12 |
-
import numpy as np
|
13 |
import time
|
14 |
|
15 |
-
# Set up OpenAI API key
|
16 |
-
openai.api_key = os.environ["CHATGPT_API_KEY"]
|
17 |
-
|
18 |
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"
|
19 |
system_message = {"role": "system", "content": Init_system_prompt}
|
20 |
|
@@ -50,30 +40,29 @@ def create_data(files):
|
|
50 |
return question_context
|
51 |
|
52 |
|
53 |
-
########### TAB 2 (CHROMA + PLOT) FUNCTIONS #############################
|
54 |
-
|
55 |
########### TAB 3 (CHAT) FUNCTIONS #############################
|
56 |
|
57 |
def user(user_message, history):
|
58 |
return "", history + [[user_message, None]]
|
59 |
|
60 |
-
def bot(history, messages_history, system_prompt, teksti_contexti, temperature, max_tokens, chatgpt_model, max_context_size_for_question):
|
61 |
user_message = history[-1][0]
|
62 |
|
63 |
-
bot_message, messages_history = ask_gpt(user_message, messages_history, system_prompt, teksti_contexti, temperature, max_tokens,chatgpt_model, max_context_size_for_question)
|
64 |
messages_history += [{"role": "assistant", "content": bot_message}]
|
65 |
history[-1][1] = bot_message
|
66 |
time.sleep(0.2)
|
67 |
return history, messages_history, str(messages_history)
|
68 |
|
69 |
-
def ask_gpt(message, messages_history, system_prompt, context, temperature, max_tokens, chatgpt_model, max_context_size_for_question):
|
70 |
messages_history, system_prompt, _ = init_history(messages_history, system_prompt)
|
71 |
if len(messages_history) < 1:
|
72 |
messages_history = [{"role": "system", "content": system_prompt}]
|
73 |
max_possible_position = len(context)- max_context_size_for_question
|
74 |
start = random.randint(0,max_possible_position)
|
75 |
messages_history += [{"role": "user", "content": context[start:start+max_context_size_for_question] + '\n Please ask a question about the previous paragramph?'}]
|
76 |
-
|
|
|
77 |
response = openai.ChatCompletion.create(
|
78 |
model=chatgpt_model,
|
79 |
messages=messages_history,
|
@@ -97,20 +86,16 @@ with gr.Blocks() as demo:
|
|
97 |
|
98 |
|
99 |
############# TAB 1 ##########################
|
100 |
-
with gr.Tab("Upload documents"):
|
|
|
|
|
101 |
with gr.Row():
|
102 |
files = gr.File(file_count='multiple', file_types=['image'], interactivate = True)
|
103 |
-
gr.
|
104 |
-
testi = gr.Button()
|
105 |
-
|
106 |
-
############# TAB 2 ##########################
|
107 |
-
with gr.Tab("Create questions"):
|
108 |
-
|
109 |
with gr.Row():
|
110 |
gr.Markdown("")
|
111 |
with gr.Row():
|
112 |
-
|
113 |
-
teksti_contexti = gr.Textbox(value='Tähän tulee konteksti', label='Dataframe columns')
|
114 |
|
115 |
############# TAB 3 ##########################
|
116 |
|
@@ -140,15 +125,12 @@ with gr.Blocks() as demo:
|
|
140 |
|
141 |
|
142 |
# TAB 1 (UPLOAD) Interactive elements:
|
143 |
-
testi.click(print_files, [files])
|
144 |
-
|
145 |
-
# TAB 2 (CHROMA + PLOT) Interactive elements:
|
146 |
create_context_btn.click(create_data, files, teksti_contexti)
|
147 |
|
148 |
|
149 |
# TAB 3 (CHAT) Interactive elements:
|
150 |
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
151 |
-
bot, [chatbot, state, system_prompt, teksti_contexti, temperature, max_tokens, chatgpt_model, max_context_size_for_question], [chatbot, state, msg_log]
|
152 |
)
|
153 |
clear.click(lambda: None, None, chatbot, queue=False).success(init_history, [state, system_prompt], [state, system_prompt, msg_log])
|
154 |
|
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
import openai
|
3 |
import pytesseract
|
4 |
import random
|
|
|
|
|
|
|
5 |
import os
|
|
|
|
|
6 |
import time
|
7 |
|
|
|
|
|
|
|
8 |
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"
|
9 |
system_message = {"role": "system", "content": Init_system_prompt}
|
10 |
|
|
|
40 |
return question_context
|
41 |
|
42 |
|
|
|
|
|
43 |
########### TAB 3 (CHAT) FUNCTIONS #############################
|
44 |
|
45 |
def user(user_message, history):
|
46 |
return "", history + [[user_message, None]]
|
47 |
|
48 |
+
def bot(history, messages_history, api_key, system_prompt, teksti_contexti, temperature, max_tokens, chatgpt_model, max_context_size_for_question):
|
49 |
user_message = history[-1][0]
|
50 |
|
51 |
+
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)
|
52 |
messages_history += [{"role": "assistant", "content": bot_message}]
|
53 |
history[-1][1] = bot_message
|
54 |
time.sleep(0.2)
|
55 |
return history, messages_history, str(messages_history)
|
56 |
|
57 |
+
def ask_gpt(message, messages_history, api_key, system_prompt, context, temperature, max_tokens, chatgpt_model, max_context_size_for_question):
|
58 |
messages_history, system_prompt, _ = init_history(messages_history, system_prompt)
|
59 |
if len(messages_history) < 1:
|
60 |
messages_history = [{"role": "system", "content": system_prompt}]
|
61 |
max_possible_position = len(context)- max_context_size_for_question
|
62 |
start = random.randint(0,max_possible_position)
|
63 |
messages_history += [{"role": "user", "content": context[start:start+max_context_size_for_question] + '\n Please ask a question about the previous paragramph?'}]
|
64 |
+
|
65 |
+
openai.api_key = api_key
|
66 |
response = openai.ChatCompletion.create(
|
67 |
model=chatgpt_model,
|
68 |
messages=messages_history,
|
|
|
86 |
|
87 |
|
88 |
############# TAB 1 ##########################
|
89 |
+
with gr.Tab("Upload documents and create context"):
|
90 |
+
with gr.Row():
|
91 |
+
api_key = gr.Textbox(value='', type='password', label='Insert OPENAI API-key here')
|
92 |
with gr.Row():
|
93 |
files = gr.File(file_count='multiple', file_types=['image'], interactivate = True)
|
94 |
+
create_context_btn = gr.Button(value='Recognize text and create context')
|
|
|
|
|
|
|
|
|
|
|
95 |
with gr.Row():
|
96 |
gr.Markdown("")
|
97 |
with gr.Row():
|
98 |
+
teksti_contexti = gr.Textbox(value='Tähän tulee konteksti', label='Created context')
|
|
|
99 |
|
100 |
############# TAB 3 ##########################
|
101 |
|
|
|
125 |
|
126 |
|
127 |
# TAB 1 (UPLOAD) Interactive elements:
|
|
|
|
|
|
|
128 |
create_context_btn.click(create_data, files, teksti_contexti)
|
129 |
|
130 |
|
131 |
# TAB 3 (CHAT) Interactive elements:
|
132 |
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
133 |
+
bot, [chatbot, state, api_key, system_prompt, teksti_contexti, temperature, max_tokens, chatgpt_model, max_context_size_for_question], [chatbot, state, msg_log]
|
134 |
)
|
135 |
clear.click(lambda: None, None, chatbot, queue=False).success(init_history, [state, system_prompt], [state, system_prompt, msg_log])
|
136 |
|