commonman commited on
Commit
cd7ff67
·
1 Parent(s): 0dcc91f

add an openAI API call to give a unit summary

Browse files
Files changed (1) hide show
  1. app.py +26 -2
app.py CHANGED
@@ -2,6 +2,9 @@ import gradio as gr
2
  import pandas as pd
3
  import random
4
  import os
 
 
 
5
 
6
  # Read global contexts
7
  with open('data/global-contexts.txt', 'r') as f:
@@ -27,11 +30,32 @@ def generate_unit(subject, year):
27
 
28
  return key_concept, related_concept, global_context
29
 
30
- iface = gr.Interface(fn=generate_unit,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  inputs=[gr.inputs.Dropdown(subjects, label="Subject"),
32
  gr.inputs.Dropdown(myp_years, label="MYP Year")],
33
  outputs=[gr.outputs.Textbox(label="Key Concept"),
34
  gr.outputs.Textbox(label="Related Concept"),
35
- gr.outputs.Textbox(label="Global Context")])
 
36
 
37
  iface.launch()
 
2
  import pandas as pd
3
  import random
4
  import os
5
+ import openai
6
+
7
+ openai.api_key = os.environ["OPEN_AI_KEY"]
8
 
9
  # Read global contexts
10
  with open('data/global-contexts.txt', 'r') as f:
 
30
 
31
  return key_concept, related_concept, global_context
32
 
33
+ def botResponse(user_input, messages):
34
+ # adds the user input to the ongoing chat log
35
+ # and submits the log to OpenAI
36
+ messages.append({"role": "user", "content": user_input})
37
+ response = openai.ChatCompletion.create(
38
+ model="gpt-3.5-turbo-0613",
39
+ messages=messages
40
+ )
41
+ return response.choices[0].message['content']
42
+
43
+ def get_llm_response_and_generate_unit(subject, year):
44
+ key_concept, related_concept, global_context = generate_unit(subject, year)
45
+ 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}'."
46
+ messages = [
47
+ {"role": "system", "content": "You are a helpful assistant."},
48
+ {"role": "user", "content": user_input}
49
+ ]
50
+ llm_response = botResponse(user_input, messages)
51
+ return key_concept, related_concept, global_context, llm_response
52
+
53
+ iface = gr.Interface(fn=get_llm_response_and_generate_unit,
54
  inputs=[gr.inputs.Dropdown(subjects, label="Subject"),
55
  gr.inputs.Dropdown(myp_years, label="MYP Year")],
56
  outputs=[gr.outputs.Textbox(label="Key Concept"),
57
  gr.outputs.Textbox(label="Related Concept"),
58
+ gr.outputs.Textbox(label="Global Context"),
59
+ gr.outputs.Textbox(label="LLM Response")])
60
 
61
  iface.launch()