Spaces:
Runtime error
Runtime error
Download app.py from commonman/Unit-Creator-v1: direct link, hf CLI and curl.
- Browser
- Download file 3.07 kB
-
https://huggingface.co/spaces/commonman/Unit-Creator-v1/resolve/main/app.py
- Command line
-
hf download hf://spaces/commonman/Unit-Creator-v1/app.py
-
curl -L -o app.py https://huggingface.co/spaces/commonman/Unit-Creator-v1/resolve/main/app.py
3.07 kB
| 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() | |