Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
#2
by
adan012
- opened
- src/streamlit_app.py +45 -39
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,46 @@
|
|
| 1 |
-
import altair as alt
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
"
|
| 28 |
-
"
|
| 29 |
-
"
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
st.
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
from PIL import Image
|
| 4 |
+
|
| 5 |
+
st.set_page_config(page_title="π€ AI Toolbox", layout="centered")
|
| 6 |
+
st.title("π€ Hugging Face Streamlit App")
|
| 7 |
+
|
| 8 |
+
# Sidebar for model choice
|
| 9 |
+
task = st.sidebar.radio("Choose a Task", ["Text Generation", "Visual QA", "Text Summarization"])
|
| 10 |
+
|
| 11 |
+
# ----------------- TEXT GENERATION -----------------
|
| 12 |
+
if task == "Text Generation":
|
| 13 |
+
st.subheader("π Text Generation (GPT-2)")
|
| 14 |
+
prompt = st.text_area("Enter a prompt", "Once upon a time in a land far away,")
|
| 15 |
+
if st.button("Generate Text"):
|
| 16 |
+
with st.spinner("Generating..."):
|
| 17 |
+
generator = pipeline("text-generation", model="openai-community/gpt2")
|
| 18 |
+
output = generator(prompt, max_length=100, num_return_sequences=1)
|
| 19 |
+
st.success("Generated Text:")
|
| 20 |
+
st.write(output[0]['generated_text'])
|
| 21 |
+
|
| 22 |
+
# ----------------- VISUAL QUESTION ANSWERING -----------------
|
| 23 |
+
elif task == "Visual QA":
|
| 24 |
+
st.subheader("πΌοΈ Visual Question Answering")
|
| 25 |
+
uploaded_image = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
| 26 |
+
question = st.text_input("Ask a question about the image", "What colors are used in this image?")
|
| 27 |
+
|
| 28 |
+
if uploaded_image and question:
|
| 29 |
+
image = Image.open(uploaded_image)
|
| 30 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 31 |
+
if st.button("Get Answer"):
|
| 32 |
+
with st.spinner("Answering..."):
|
| 33 |
+
vqa = pipeline("visual-question-answering", model="dandelin/vilt-b32-finetuned-vqa")
|
| 34 |
+
result = vqa(image, question)
|
| 35 |
+
st.success(f"Answer: {result[0]['answer']}")
|
| 36 |
+
|
| 37 |
+
# ----------------- TEXT SUMMARIZATION -----------------
|
| 38 |
+
elif task == "Text Summarization":
|
| 39 |
+
st.subheader("π Text Summarization")
|
| 40 |
+
input_text = st.text_area("Paste long text here", height=200)
|
| 41 |
+
if st.button("Summarize"):
|
| 42 |
+
with st.spinner("Summarizing..."):
|
| 43 |
+
summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")
|
| 44 |
+
summary = summarizer(input_text, max_length=130, min_length=30, do_sample=False)
|
| 45 |
+
st.success("Summary:")
|
| 46 |
+
st.write(summary[0]['summary_text'])
|