ferguch9 commited on
Commit
d2e13e2
·
1 Parent(s): e346c8d

feat: models

Browse files
Files changed (4) hide show
  1. app.py +32 -7
  2. chat_openai.py +18 -0
  3. requirements.txt +5 -1
  4. slider.py +4 -0
app.py CHANGED
@@ -1,13 +1,38 @@
1
  import streamlit as st
2
  from transformers import pipeline
3
 
4
- x = st.slider('Select a value and I\'ll square it')
5
- st.write(x, 'squared is', x * x)
6
 
7
- pipe = pipeline('sentiment-analysis')
8
- text = st.text_area('Enter some text and I\'ll run sentiment analysis on it')
 
 
9
 
10
- if text:
11
- out = pipe(text)
12
- st.json(out)
13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
  from transformers import pipeline
3
 
 
 
4
 
5
+ def build_pipeline(text : str, model : str):
6
+ st.text(f"Using model {model}")
7
+ classifier = pipeline(task=text, model=model)
8
+ return classifier
9
 
 
 
 
10
 
11
+ st.header('Pipeline Examples', divider='rainbow')
12
+ st.subheader('Enter some text to demonstrate different pipeline/model analysis', divider='grey')
13
+ option = st.selectbox('Select model task:', ('sentiment-analysis', 'summarization', 'text-generation', 'translation', 'zero-shot-classification'))
14
+ text = st.text_area('Text to analyse:', "Star Trek Enterprise follows the adventures of the crew of the first starship Enterprise, designation NX-01. They are the first deep space explorers in Starfleet, using the first warp five capable vessel. The Vulcans have withheld advanced technology from humanity since their first contact, concerned that humans were not ready for it. This has delayed human space exploration and caused resentment in Starfleet test pilot Jonathan Archer, whose father developed the Warp 5 engine but did not live to see it used.", height=200)
15
+ st.write(f'{len(text)} characters')
16
+ st.button('Analyse', type='primary')
17
+ st.subheader(f'{option}', divider='grey')
18
+
19
+ output = {}
20
+ with st.spinner("Analysing..."):
21
+ if option == 'sentiment-analysis':
22
+ classifier = build_pipeline(text=option, model='distilbert-base-uncased-finetuned-sst-2-english')
23
+ output = classifier(text)
24
+ elif option == 'summarization':
25
+ classifier = build_pipeline(text=option, model='facebook/bart-large-cnn')
26
+ output = classifier(text, min_length=5, max_length=50, do_sample=False)
27
+ elif option == 'text-generation':
28
+ classifier = build_pipeline(text=option, model='distilgpt2')
29
+ output = classifier(text)
30
+ elif option == 'zero-shot-classification':
31
+ classifier = build_pipeline(text=option, model='distilgpt2')
32
+ output = classifier(text, candidate_labels=["space", "dicovery", "humans"])
33
+ elif option == 'translation':
34
+ classifier = build_pipeline(text="translation", model='Helsinki-NLP/opus-mt-en-es')
35
+ output = classifier(text)
36
+ st.success("Complete")
37
+
38
+ st.json(output)
chat_openai.py ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from langchain.llms import OpenAI
3
+
4
+ st.title('🦜🔗 Quickstart OpenAI App')
5
+
6
+ openai_api_key = st.sidebar.text_input('OpenAI API Key', type='password')
7
+
8
+ def generate_response(input_text):
9
+ llm = OpenAI(temperature=0.7, openai_api_key=openai_api_key)
10
+ st.info(llm(input_text))
11
+
12
+ with st.form('my_form'):
13
+ text = st.text_area('Enter text:', 'Ask me a question?')
14
+ submitted = st.form_submit_button('Submit')
15
+ if not openai_api_key.startswith('sk-'):
16
+ st.warning('Please enter your OpenAI API key!', icon='⚠')
17
+ if submitted and openai_api_key.startswith('sk-'):
18
+ generate_response(text)
requirements.txt CHANGED
@@ -1,2 +1,6 @@
 
 
 
 
1
  torch
2
- transformers
 
1
+ openai
2
+ langchain
3
+ sentencepiece
4
+ streamlit
5
  torch
6
+ transformers
slider.py ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ import streamlit as st
2
+
3
+ x = st.slider('Select a value and I\'ll square it')
4
+ st.write(x, 'squared is', x * x)