Update src/streamlit_app.py

#2
by adan012 - opened
Files changed (1) hide show
  1. 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
- # Welcome to Streamlit!
8
-
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
-
13
- In the meantime, below is an example of what you can do with just a few lines of code:
14
- """
15
-
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
18
-
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
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'])