File size: 3,074 Bytes
3f67f25
aa7e9fd
2caee47
9eabf11
cd7ff67
 
fba560a
9eabf11
aa7e9fd
 
 
3f67f25
aa7e9fd
 
2caee47
aa7e9fd
 
b20bc5a
 
aa7e9fd
 
 
 
 
 
 
4d61f58
 
2caee47
cd7ff67
 
 
 
 
 
 
 
b20bc5a
cd7ff67
 
 
 
 
 
b20bc5a
 
3e39ade
645e9a0
 
 
 
 
 
 
3e39ade
 
645e9a0
 
 
 
 
 
3e39ade
861b4d5
3e39ade
 
2ce80ff
 
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
import gradio as gr
import pandas as pd
import random
import os
import openai

openai.api_key = os.environ["OPENAI_API_KEY"]

# Read global contexts
with open('data/global-contexts.txt', 'r') as f:
    global_contexts = f.read().splitlines()

# Read key and related concepts
df = pd.read_csv('data/MYP_Key_Related_Concepts.tsv', sep='\t')

# Get subjects and MYP years
subjects = df['Subject'].tolist()
myp_years = ["MYP 1 / Grade 6", "MYP 2 / Grade 7", "MYP 3 / Grade 8", "MYP 4 / Grade 9", "MYP 5 / Grade 10"]

def generate_unit(subject, year):
    row = df.loc[df['Subject'] == subject].squeeze()
    key_concepts = row.filter(regex='Key.*').dropna().tolist()
    related_concepts = row.filter(regex='Related.*').dropna().tolist()
    key_concept = random.choice(key_concepts)
    related_concept = random.choice(related_concepts)
    global_context = random.choice(global_contexts)
    return subject, year, key_concept, related_concept, global_context


def botResponse(user_input, messages):
    messages.append({"role": "user", "content": user_input})
    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo-0613",
        messages=messages
    )
    return response.choices[0].message['content']

def get_llm_response_and_generate_unit(subject, year, key_concept, related_concept, global_context):
    user_input = f"We are studying the subject '{subject}' with a focus on the key concept '{key_concept}', the related concept '{related_concept}', and under the global context of '{global_context}'."
    messages = [
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": user_input}
    ]
    llm_response = botResponse(user_input, messages)
    return llm_response

iface1 = gr.Interface(fn=generate_unit,
                      inputs=[gr.Dropdown(choices=subjects, label="Subject", default=subjects[0]),
                              gr.Dropdown(choices=myp_years, label="MYP Year", default=myp_years[0])],
                      outputs=[gr.Textbox(label="Subject"),
                               gr.Textbox(label="MYP Year"),
                               gr.Textbox(label="Key Concept"),
                               gr.Textbox(label="Related Concept"),
                               gr.Textbox(label="Global Context")])

iface2 = gr.Interface(fn=get_llm_response_and_generate_unit,
                      inputs=[gr.Textbox(label="Subject"),
                              gr.Textbox(label="MYP Year"),
                              gr.Textbox(label="Key Concept"),
                              gr.Textbox(label="Related Concept"),
                              gr.Textbox(label="Global Context")],
                      outputs=gr.Textbox(label="LLM Response"))

iface = gr.Series(iface1, iface2,
                  title="Unit Idea Generator (MYP Version)",
                  description="Step 1: Select a subject and MYP year to generate a unit idea. Step 2: Once you're satisfied with the generated unit idea, use it to generate a response from the language model.")

iface.launch()