adan012 commited on
Commit
fd50f79
Β·
verified Β·
1 Parent(s): 2aee335

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +34 -38
src/streamlit_app.py CHANGED
@@ -2,45 +2,41 @@ 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="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'])
 
2
  from transformers import pipeline
3
  from PIL import Image
4
 
5
+
6
+ st.title("Multimodal AI App πŸ€–")
7
+ st.sidebar.header("πŸ”§ Choose Task")
8
 
9
+ task = st.sidebar.selectbox("πŸ“‚ Select task", ["πŸ–ΌοΈ Visual Question Answering", "🌐 Translate to Urdu", "πŸ“– Story Generator"])
 
10
 
11
+ if task == "πŸ–ΌοΈ Visual Question Answering":
12
+ st.header("πŸ–ΌοΈ Visual Question Answering")
13
+ uploaded_file = st.file_uploader("πŸ“€ Upload an image", type=["jpg", "png", "jpeg"])
14
+ question = st.text_input("❓ Ask a question about the image")
15
+ if uploaded_file and question:
16
+ image = Image.open(uploaded_file)
17
+ if st.button("πŸ” Ask Question"):
18
+ with st.spinner('⏳ Loading VQA model...'):
19
+ vqa_pipe = pipeline("visual-question-answering", model="dandelin/vilt-b32-finetuned-vqa")
20
+ result = vqa_pipe(image, question)
21
+ st.image(image, caption="πŸ–ΌοΈ Uploaded Image")
22
+ st.success(f"βœ… **Answer:** {result[0]['answer']}")
23
+
24
+ elif task == "🌐 Translate to Urdu":
25
+ st.header("🌐 English to Urdu Translation")
26
+ input_text = st.text_area("✏️ Enter English text")
27
+ if st.button("🌍 Translate"):
28
+ with st.spinner('⏳ Loading Translation model...'):
29
+ translator = pipeline("translation", model="facebook/nllb-200-distilled-600M")
30
+ translation = translator(input_text, src_lang="eng_Latn", tgt_lang="urd_Arab")
31
+ st.success(f"βœ… **Urdu Translation:** {translation[0]['translation_text']}")
32
+
33
+ elif task == "πŸ“– Story Generator":
34
+ st.header("πŸ“ Story Generator")
35
+ prompt = st.text_input("πŸ’‘ Enter a prompt")
36
+ if st.button("✍️ Generate Story"):
37
+ with st.spinner('⏳ Loading Text Generation model...'):
38
+ text_gen_pipe = pipeline("text-generation", model="openai-community/gpt2")
39
+ result = text_gen_pipe(prompt, max_length=100, num_return_sequences=1)
40
+ st.success(f"βœ… **Generated Text:** {result[0]['generated_text']}")
41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42