Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| from transformers import pipeline | |
| ################################ | |
| ####### Generic functions ###### | |
| ################################ | |
| def option_change(): | |
| if st.session_state.action_type=="Sentiment Analysis": | |
| sentiment_analysis() | |
| elif st.session_state.action_type=="Zero-shot Classification": | |
| zero_shot_classification() | |
| elif st.session_state.action_type=="Text Generation": | |
| text_generation() | |
| elif st.session_state.action_type=="Mask Filling": | |
| mask_filling() | |
| elif st.session_state.action_type=="Named Entity Recognition": | |
| named_entity_recognition() | |
| elif st.session_state.action_type=="Question & Answering": | |
| qna() | |
| elif st.session_state.action_type=="Summarization": | |
| summarization() | |
| elif st.session_state.action_type=="English to French Translation": | |
| translation_eng2frn() | |
| def button_clicked(): | |
| if st.session_state.action_type=="Sentiment Analysis": | |
| sentiment_analysis_btn() | |
| elif st.session_state.action_type=="Zero-shot Classification": | |
| zero_shot_classification_btn() | |
| elif st.session_state.action_type=="Text Generation": | |
| text_generation_btn() | |
| elif st.session_state.action_type=="Mask Filling": | |
| mask_filling_btn() | |
| elif st.session_state.action_type=="Named Entity Recognition": | |
| named_entity_recognition_btn() | |
| elif st.session_state.action_type=="Question & Answering": | |
| qna_btn() | |
| elif st.session_state.action_type=="Summarization": | |
| summarization_btn() | |
| elif st.session_state.action_type=="English to French Translation": | |
| translation_eng2frn_btn() | |
| def sentiment_analysis(): | |
| st.session_state.input_text="I've been waiting for a HuggingFace course my whole life." | |
| st.session_state.output_text="" | |
| def sentiment_analysis_btn(): | |
| classifier = pipeline("sentiment-analysis") | |
| st.session_state.output_text=str(classifier(st.session_state.input_text)) | |
| def zero_shot_classification(): | |
| st.session_state.input_text="This is a course about the transformers library" | |
| st.session_state.output_text="" | |
| def zero_shot_classification_btn(): | |
| classifier = pipeline("zero-shot-classification") | |
| st.session_state.output_text=str(classifier( | |
| st.session_state.input_text, | |
| candidate_labels=["education", "politics", "business"], | |
| )) | |
| def text_generation(): | |
| st.session_state.input_text="In this course, we will teach you how to" | |
| st.session_state.output_text="" | |
| def text_generation_btn(): | |
| generator = pipeline("text-generation") | |
| st.session_state.output_text=str(generator(st.session_state.input_text)) | |
| def mask_filling(): | |
| st.session_state.input_text="This course will teach you all about <mask> models." | |
| st.session_state.output_text="" | |
| def mask_filling_btn(): | |
| unmasker = pipeline("fill-mask") | |
| st.session_state.output_text=str(unmasker(st.session_state.input_text, top_k=2)) | |
| def named_entity_recognition(): | |
| st.session_state.input_text="My name is Sylvain and I work at Hugging Face in Brooklyn." | |
| st.session_state.output_text="" | |
| def named_entity_recognition_btn(): | |
| ner = pipeline("ner", grouped_entities=True) | |
| st.session_state.output_text=str(ner(st.session_state.input_text)) | |
| def qna(): | |
| st.session_state.input_text="My name is Sylvain and I work at Hugging Face in Brooklyn" | |
| st.session_state.output_text="" | |
| def qna_btn(): | |
| question_answerer = pipeline("question-answering") | |
| st.session_state.output_text="Questionn: Where do I work?\n Answer: " | |
| st.session_state.output_text=st.session_state.output_text + str(question_answerer( | |
| question=st.session_state.input_text, | |
| context="My name is Sylvain and I work at Hugging Face in Brooklyn", | |
| )) | |
| def summarization(): | |
| st.session_state.input_text= """America has changed dramatically during recent years. Not only has the number of | |
| graduates in traditional engineering disciplines such as mechanical, civil, | |
| electrical, chemical, and aeronautical engineering declined, but in most of | |
| the premier American universities engineering curricula now concentrate on | |
| and encourage largely the study of engineering science. As a result, there | |
| are declining offerings in engineering subjects dealing with infrastructure, | |
| the environment, and related issues, and greater concentration on high | |
| technology subjects, largely supporting increasingly complex scientific | |
| developments. While the latter is important, it should not be at the expense | |
| of more traditional engineering. | |
| Rapidly developing economies such as China and India, as well as other | |
| industrial countries in Europe and Asia, continue to encourage and advance | |
| the teaching of engineering. Both China and India, respectively, graduate | |
| six and eight times as many traditional engineers as does the United States. | |
| Other industrial countries at minimum maintain their output, while America | |
| suffers an increasingly serious decline in the number of engineering graduates | |
| and a lack of well-educated engineers. | |
| """ | |
| st.session_state.output_text="" | |
| def summarization_btn(): | |
| summarizer = pipeline("summarization") | |
| st.session_state.output_text=str(summarizer(st.session_state.input_text)) | |
| def translation_eng2frn(): | |
| st.session_state.input_text= "This course is produced by Hugging Face." | |
| st.session_state.output_text="" | |
| def translation_eng2frn_btn(): | |
| translator = pipeline("translation", model="Helsinki-NLP/opus-mt-en-fr") | |
| st.session_state.output_text=str(translator(st.session_state.input_text)) | |
| ################################ | |
| ####### Display of data ######## | |
| ################################ | |
| st.set_page_config(layout='wide') | |
| # Title | |
| st.title("LLM Basics Demo") | |
| # 3 Column Layout | |
| col1, col2, col3 = st.columns(3) | |
| with col1: | |
| options = ["Sentiment Analysis", "Zero-shot Classification", "Text Generation","Mask Filling","Named Entity Recognition","Question & Answering","Summarization","English to French Translation"] | |
| llm_action_type = st.selectbox("Select LLM Tasktype", options,key="action_type", on_change=option_change) | |
| with col3: | |
| llm_button = st.button("Generate Data",on_click=button_clicked) | |
| # Display Input in single Layout | |
| llm_input_text = st.text_area(label="Enter your message",value="I've been waiting for a HuggingFace course my whole life.",key="input_text", height=400) | |
| # Display Output in single Layout | |
| llm_output_text = st.text_area("Generate Output",key="output_text", height=400) |