Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import pipeline
|
3 |
+
|
4 |
+
# Step 1: Load the Hugging Face model
|
5 |
+
@st.cache_resource
|
6 |
+
def load_model():
|
7 |
+
return pipeline("text-generation", model="gpt2") # Replace 'gpt2' with another model if needed
|
8 |
+
|
9 |
+
generator = load_model()
|
10 |
+
|
11 |
+
# Step 2: Design the Streamlit layout
|
12 |
+
st.title("Hugging Face Text Generator")
|
13 |
+
st.write("Generate creative text using GPT-2!")
|
14 |
+
|
15 |
+
# Get user input
|
16 |
+
user_input = st.text_area("Enter a prompt for text generation:", "Once upon a time")
|
17 |
+
|
18 |
+
# Generate text when the button is clicked
|
19 |
+
if st.button("Generate Text"):
|
20 |
+
with st.spinner("Generating..."):
|
21 |
+
results = generator(user_input, max_length=50, num_return_sequences=1)
|
22 |
+
generated_text = results[0]["generated_text"]
|
23 |
+
st.subheader("Generated Text:")
|
24 |
+
st.write(generated_text)
|
25 |
+
|
26 |
+
st.write("Powered by Streamlit and Hugging Face 🤗")
|
27 |
+
|
28 |
+
|
29 |
+
|
30 |
+
import streamlit as st
|
31 |
+
from transformers import pipeline
|
32 |
+
from PIL import Image
|
33 |
+
|
34 |
+
# Load Hugging Face models
|
35 |
+
@st.cache_resource
|
36 |
+
def load_image_classifier():
|
37 |
+
return pipeline("image-classification", model="google/vit-base-patch16-224")
|
38 |
+
|
39 |
+
@st.cache_resource
|
40 |
+
def load_text_classifier():
|
41 |
+
return pipeline("sentiment-analysis") # Default model for sentiment analysis
|
42 |
+
|
43 |
+
# Initialize models
|
44 |
+
image_classifier = load_image_classifier()
|
45 |
+
text_classifier = load_text_classifier()
|
46 |
+
|
47 |
+
# App title and navigation
|
48 |
+
st.title("Hugging Face Classification App")
|
49 |
+
st.sidebar.title("Choose Task")
|
50 |
+
task = st.sidebar.selectbox("Select a task", ["Image Classification", "Text Classification"])
|
51 |
+
|
52 |
+
if task == "Image Classification":
|
53 |
+
st.header("Image Classification")
|
54 |
+
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
55 |
+
if uploaded_file is not None:
|
56 |
+
# Display uploaded image
|
57 |
+
image = Image.open(uploaded_file)
|
58 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
59 |
+
|
60 |
+
# Classify the image
|
61 |
+
if st.button("Classify Image"):
|
62 |
+
with st.spinner("Classifying..."):
|
63 |
+
results = image_classifier(image)
|
64 |
+
st.subheader("Classification Results")
|
65 |
+
for result in results:
|
66 |
+
st.write(f"**{result['label']}**: {result['score']:.2f}")
|
67 |
+
|
68 |
+
elif task == "Text Classification":
|
69 |
+
st.header("Text Classification")
|
70 |
+
text_input = st.text_area("Enter text for classification", "Streamlit is an amazing tool!")
|
71 |
+
|
72 |
+
# Classify the text
|
73 |
+
if st.button("Classify Text"):
|
74 |
+
with st.spinner("Classifying..."):
|
75 |
+
results = text_classifier(text_input)
|
76 |
+
st.subheader("Classification Results")
|
77 |
+
for result in results:
|
78 |
+
st.write(f"**{result['label']}**: {result['score']:.2f}")
|
79 |
+
|
80 |
+
st.write("Powered by Streamlit and Hugging Face 🤗")
|
81 |
+
|
82 |
+
|
83 |
+
|
84 |
+
|
85 |
+
|
86 |
+
|
87 |
+
|
88 |
+
|
89 |
+
|
90 |
+
|