Spaces:
Runtime error
Runtime error
Commit
·
693a64e
0
Parent(s):
Duplicate from mutukrish/eng-to-mql
Browse files- .gitattributes +34 -0
- README.md +14 -0
- app.py +197 -0
- requirements.txt +4 -0
.gitattributes
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
28 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
29 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
30 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
31 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
32 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
33 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
34 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: Eng To Mql
|
3 |
+
emoji: 🏃
|
4 |
+
colorFrom: yellow
|
5 |
+
colorTo: indigo
|
6 |
+
sdk: streamlit
|
7 |
+
sdk_version: 1.17.0
|
8 |
+
app_file: app.py
|
9 |
+
pinned: false
|
10 |
+
license: apache-2.0
|
11 |
+
duplicated_from: mutukrish/eng-to-mql
|
12 |
+
---
|
13 |
+
|
14 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,197 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import os
|
3 |
+
import openai
|
4 |
+
from pymongo import MongoClient
|
5 |
+
from datetime import datetime
|
6 |
+
import random
|
7 |
+
|
8 |
+
# Schema Versions
|
9 |
+
# 1. First version, using text-davinci-003 model
|
10 |
+
# 2. Switched to gpt-3.5-turbo model
|
11 |
+
# 3. Logging the model as well
|
12 |
+
|
13 |
+
|
14 |
+
# you need to set your OpenAI API key as environment variable
|
15 |
+
openai.api_key = st.secrets["API_KEY"]
|
16 |
+
|
17 |
+
MOVIES_EXAMPLE_DOC = """{
|
18 |
+
_id: ObjectId("573a1390f29313caabcd4135"),
|
19 |
+
genres: [ 'Short' ],
|
20 |
+
runtime: 1,
|
21 |
+
cast: [ 'Charles Kayser', 'John Ott' ],
|
22 |
+
num_mflix_comments: 0,
|
23 |
+
title: 'Blacksmith Scene',
|
24 |
+
countries: [ 'USA' ],
|
25 |
+
released: ISODate("1893-05-09T00:00:00.000Z"),
|
26 |
+
directors: [ 'William K.L. Dickson' ],
|
27 |
+
rated: 'UNRATED',
|
28 |
+
awards: { wins: 1, nominations: 0, text: '1 win.' },
|
29 |
+
lastupdated: '2015-08-26 00:03:50.133000000',
|
30 |
+
year: 1893,
|
31 |
+
imdb: { rating: 6.2, votes: 1189, id: 5 },
|
32 |
+
type: 'movie',
|
33 |
+
tomatoes: {
|
34 |
+
viewer: { rating: 3, numReviews: 184, meter: 32 },
|
35 |
+
lastUpdated: ISODate("2015-06-28T18:34:09.000Z")
|
36 |
+
}
|
37 |
+
}"""
|
38 |
+
|
39 |
+
MOVIES_EXAMPLE_QUESTIONS = [
|
40 |
+
(
|
41 |
+
"How many fantasy or horror movies from the USA with an imdb rating "
|
42 |
+
"greater than 6.0 are there in this dataset?"
|
43 |
+
),
|
44 |
+
(
|
45 |
+
"Which movies were released on a Monday and have a higher tomato rating "
|
46 |
+
"than IMDB rating? Keep in mind that IMDB goes from 1-10 and tomatoes "
|
47 |
+
"only from 1-5, so you need to normalise the ratings to do a fair comparison."
|
48 |
+
),
|
49 |
+
"What movies should I watch to learn more about Japanse culture?",
|
50 |
+
(
|
51 |
+
"How many movies were released in each decade? Write decade as a string, e.g. "
|
52 |
+
"'1920-1929'. Sort ascending by decade."
|
53 |
+
),
|
54 |
+
(
|
55 |
+
"Find movies that are suitable to watch with my kids, both by genre and their "
|
56 |
+
"parental guidance rating. Just recommend good movies."
|
57 |
+
),
|
58 |
+
]
|
59 |
+
|
60 |
+
BASE_CHAT_MESSAGES = [
|
61 |
+
{
|
62 |
+
"role": "system",
|
63 |
+
"content": "You are an expert English to MongoDB aggregation pipeline translation system."
|
64 |
+
"You will accept an example document from a collection and an English question, and return an aggregation "
|
65 |
+
"pipeline that can answer the question. Do not explain the query or add any additional comments, only "
|
66 |
+
"return a single code block with the aggregation pipeline without the aggregate command.",
|
67 |
+
}
|
68 |
+
]
|
69 |
+
|
70 |
+
MODEL_NAME = "gpt-3.5-turbo"
|
71 |
+
|
72 |
+
|
73 |
+
@st.cache
|
74 |
+
def ask_model(doc, question):
|
75 |
+
"""This is the call to the OpenAI API. It creates a prompt from the document
|
76 |
+
and question and returns the endpoint's response."""
|
77 |
+
|
78 |
+
messages = BASE_CHAT_MESSAGES + [
|
79 |
+
{
|
80 |
+
"role": "user",
|
81 |
+
"content": f"Example document: {doc.strip()}\n\nQuestion: {question.strip()}\n\n",
|
82 |
+
}
|
83 |
+
]
|
84 |
+
|
85 |
+
return openai.ChatCompletion.create(
|
86 |
+
model=MODEL_NAME,
|
87 |
+
messages=messages,
|
88 |
+
temperature=0,
|
89 |
+
max_tokens=1000,
|
90 |
+
top_p=1.0,
|
91 |
+
)
|
92 |
+
|
93 |
+
|
94 |
+
def extract_pipeline(response):
|
95 |
+
content = response["choices"][0]["message"]["content"].strip("\n `")
|
96 |
+
return content
|
97 |
+
|
98 |
+
|
99 |
+
st.set_page_config(layout="wide")
|
100 |
+
|
101 |
+
# initialise session state
|
102 |
+
if not "response" in st.session_state:
|
103 |
+
st.session_state.response = None
|
104 |
+
if not "_id" in st.session_state:
|
105 |
+
st.session_state._id = None
|
106 |
+
if not "feedback" in st.session_state:
|
107 |
+
st.session_state.feedback = False
|
108 |
+
if not "default_question" in st.session_state:
|
109 |
+
st.session_state.default_question = random.choice(MOVIES_EXAMPLE_QUESTIONS)
|
110 |
+
|
111 |
+
# DB access
|
112 |
+
|
113 |
+
|
114 |
+
st.markdown(
|
115 |
+
"""# English to MQL Demo
|
116 |
+
|
117 |
+
This demo app uses OpenAI's GPT-4 (gpt-4) model to generate a MongoDB
|
118 |
+
aggregation pipeline from an English question and example document.
|
119 |
+
|
120 |
+
🚧 The app is experimental and may return incorrect results. Do not enter any sensitive information! 🚧
|
121 |
+
"""
|
122 |
+
)
|
123 |
+
|
124 |
+
|
125 |
+
# two-column layout
|
126 |
+
col_left, col_right = st.columns(2, gap="large")
|
127 |
+
|
128 |
+
with col_left:
|
129 |
+
st.markdown("### Example Document and Question")
|
130 |
+
# wrap textareas in form
|
131 |
+
with st.form("text_inputs"):
|
132 |
+
doc = st.text_area(
|
133 |
+
"Enter example document from collection, e.g. db.collection.findOne()",
|
134 |
+
value=MOVIES_EXAMPLE_DOC,
|
135 |
+
height=300,
|
136 |
+
)
|
137 |
+
|
138 |
+
# question textarea
|
139 |
+
question = st.text_area(
|
140 |
+
label="Ask question in English",
|
141 |
+
value=st.session_state.default_question,
|
142 |
+
)
|
143 |
+
|
144 |
+
# submit button
|
145 |
+
submitted = st.form_submit_button("Translate", type="primary")
|
146 |
+
if submitted:
|
147 |
+
st.session_state._id = None
|
148 |
+
st.session_state.feedback = False
|
149 |
+
st.session_state.response = ask_model(doc, question)
|
150 |
+
|
151 |
+
|
152 |
+
with col_right:
|
153 |
+
st.markdown("### Generated MQL")
|
154 |
+
|
155 |
+
# show response
|
156 |
+
response = st.session_state.response
|
157 |
+
if response:
|
158 |
+
pipeline = extract_pipeline(response)
|
159 |
+
# print result as code block
|
160 |
+
st.code(
|
161 |
+
pipeline,
|
162 |
+
language="javascript",
|
163 |
+
)
|
164 |
+
|
165 |
+
# feedback form
|
166 |
+
with st.empty():
|
167 |
+
if st.session_state.feedback:
|
168 |
+
st.write("✅ Thank you for your feedback.")
|
169 |
+
|
170 |
+
elif st.session_state._id:
|
171 |
+
with st.form("feedback_inputs"):
|
172 |
+
radio = st.radio("Is the result correct?", ("Yes", "No"))
|
173 |
+
feedback = st.text_area(
|
174 |
+
"If not, please tell us what the issue is:",
|
175 |
+
)
|
176 |
+
|
177 |
+
# submit button
|
178 |
+
feedback_submit = st.form_submit_button(
|
179 |
+
"Submit Feedback", type="secondary"
|
180 |
+
)
|
181 |
+
if feedback_submit:
|
182 |
+
st.session_state.feedback = {
|
183 |
+
"correct": radio == "Yes",
|
184 |
+
"comment": feedback,
|
185 |
+
}
|
186 |
+
|
187 |
+
else:
|
188 |
+
doc = {
|
189 |
+
"ts": datetime.now(),
|
190 |
+
"doc": doc,
|
191 |
+
"question": question,
|
192 |
+
"generated_mql": pipeline,
|
193 |
+
"response": response,
|
194 |
+
"version": 3,
|
195 |
+
"model": MODEL_NAME,
|
196 |
+
}
|
197 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
openai==0.27.0
|
2 |
+
streamlit==1.17.0
|
3 |
+
pymongo==4.3.3
|
4 |
+
watchdog==3.0.0
|