Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,68 +3,65 @@ import requests
|
|
| 3 |
import streamlit as st
|
| 4 |
import openai
|
| 5 |
|
| 6 |
-
|
| 7 |
-
st.title("Scientific Question Generation")
|
| 8 |
-
|
| 9 |
-
checkpoints = ['dhmeltzer/bart-large_askscience-qg',
|
| 10 |
-
'dhmeltzer/flan-t5-base_askscience-qg',
|
| 11 |
-
'google/flan-t5-xxl']
|
| 12 |
-
|
| 13 |
-
headers = {"Authorization": f"Bearer {st.secrets['HF_token']}"}
|
| 14 |
-
openai.api_key = st.secrets['OpenAI_token']
|
| 15 |
-
|
| 16 |
-
def query(checkpoint, payload):
|
| 17 |
-
API_URL = f"https://api-inference.huggingface.co/models/{checkpoint}"
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
if user_input:
|
| 30 |
-
for checkpoint in checkpoints:
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
prompt = 'generate a question: ' + user_input
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
model_engine = "gpt-3.5-turbo"
|
| 52 |
-
max_tokens = 50
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
{"role": "user", "content": prompt},
|
| 61 |
-
])
|
| 62 |
-
|
| 63 |
-
output = response['choices'][0]['message']['content']
|
| 64 |
|
| 65 |
-
|
|
|
|
|
|
|
| 66 |
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
| 70 |
#[0]['generated_text']
|
|
|
|
| 3 |
import streamlit as st
|
| 4 |
import openai
|
| 5 |
|
| 6 |
+
def main():
|
| 7 |
+
st.title("Scientific Question Generation")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
checkpoints = ['dhmeltzer/bart-large_askscience-qg',
|
| 10 |
+
'dhmeltzer/flan-t5-base_askscience-qg',
|
| 11 |
+
'google/flan-t5-xxl']
|
| 12 |
|
| 13 |
+
headers = {"Authorization": f"Bearer {st.secrets['HF_token']}"}
|
| 14 |
+
openai.api_key = st.secrets['OpenAI_token']
|
| 15 |
+
|
| 16 |
+
def query(checkpoint, payload):
|
| 17 |
+
API_URL = f"https://api-inference.huggingface.co/models/{checkpoint}"
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
response = requests.post(API_URL,
|
| 20 |
+
headers=headers,
|
| 21 |
+
json=payload)
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
return response.json()
|
| 24 |
+
|
| 25 |
+
# User search
|
| 26 |
+
user_input = st.text_area("Question Generator",
|
| 27 |
+
"""Black holes are the most gravitationally dense objects in the universe.""")
|
| 28 |
+
|
| 29 |
+
if user_input:
|
| 30 |
+
for checkpoint in checkpoints:
|
| 31 |
+
|
| 32 |
+
model_name = checkpoint.split('/')[1]
|
| 33 |
+
|
| 34 |
+
if 'flan' in model_name.lower():
|
| 35 |
+
|
| 36 |
+
prompt = 'generate a question: ' + user_input
|
| 37 |
|
| 38 |
+
try:
|
| 39 |
+
output = query(checkpoint,{
|
| 40 |
+
"inputs": prompt,
|
| 41 |
+
"wait_for_model":True})[0]['generated_text']
|
| 42 |
+
except:
|
| 43 |
+
st.write(output)
|
| 44 |
+
return
|
| 45 |
|
| 46 |
+
st.write(f'Model {model_name}: {output}')
|
| 47 |
+
|
| 48 |
+
model_engine = "gpt-3.5-turbo"
|
| 49 |
+
max_tokens = 50
|
| 50 |
|
| 51 |
+
prompt = f"generate a question: {user_input}"
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
+
response=openai.ChatCompletion.create(
|
| 54 |
+
model=model_engine,
|
| 55 |
+
messages=[
|
| 56 |
+
{"role": "system", "content": "You are a helpful assistant that generates questions from text."},
|
| 57 |
+
{"role": "user", "content": prompt},
|
| 58 |
+
])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
+
output = response['choices'][0]['message']['content']
|
| 61 |
+
|
| 62 |
+
st.write(f'Model {model_engine}: {output}')
|
| 63 |
|
| 64 |
|
| 65 |
+
if __name__ == "__main__":
|
| 66 |
+
main()
|
| 67 |
#[0]['generated_text']
|