Mohuu0601 commited on
Commit
5482396
·
verified ·
1 Parent(s): d60eec7

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -0
app.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import pipeline
3
+ from PIL import Image
4
+
5
+ # Load Hugging Face models
6
+ @st.cache_resource
7
+ def load_image_classifier():
8
+ return pipeline("image-classification", model="google/vit-base-patch16-224")
9
+
10
+ @st.cache_resource
11
+ def load_text_classifier():
12
+ return pipeline("sentiment-analysis") # Default model for sentiment analysis
13
+
14
+ # Initialize models
15
+ image_classifier = load_image_classifier()
16
+ text_classifier = load_text_classifier()
17
+
18
+ # App title and navigation
19
+ st.title("Hugging Face Classification App")
20
+ st.sidebar.title("Choose Task")
21
+ task = st.sidebar.selectbox("Select a task", ["Image Classification", "Text Classification"])
22
+
23
+ if task == "Image Classification":
24
+ st.header("Image Classification")
25
+ uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
26
+ if uploaded_file is not None:
27
+ # Display uploaded image
28
+ image = Image.open(uploaded_file)
29
+ st.image(image, caption="Uploaded Image", use_column_width=True)
30
+
31
+ # Classify the image
32
+ if st.button("Classify Image"):
33
+ with st.spinner("Classifying..."):
34
+ results = image_classifier(image)
35
+ st.subheader("Classification Results")
36
+ for result in results:
37
+ st.write(f"**{result['label']}**: {result['score']:.2f}")
38
+
39
+ elif task == "Text Classification":
40
+ st.header("Text Classification")
41
+ text_input = st.text_area("Enter text for classification", "Streamlit is an amazing tool!")
42
+
43
+ # Classify the text
44
+ if st.button("Classify Text"):
45
+ with st.spinner("Classifying..."):
46
+ results = text_classifier(text_input)
47
+ st.subheader("Classification Results")
48
+ for result in results:
49
+ st.write(f"**{result['label']}**: {result['score']:.2f}")
50
+
51
+ st.write("Powered by Streamlit and Hugging Face 🤗")