Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +55 -0
- requirements.txt +5 -0
app.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import streamlit as st
|
3 |
+
st.set_page_config(page_title="Explain Like I'm 5", page_icon="🧸", layout="centered")
|
4 |
+
|
5 |
+
from transformers import pipeline
|
6 |
+
from fpdf import FPDF
|
7 |
+
|
8 |
+
# Load Alpaca model (CPU-safe)
|
9 |
+
@st.cache_resource
|
10 |
+
def load_model():
|
11 |
+
return pipeline("text2text-generation", model="declare-lab/flan-alpaca-base")
|
12 |
+
|
13 |
+
generator = load_model()
|
14 |
+
|
15 |
+
# Page Header
|
16 |
+
st.markdown("<h1 style='text-align: center;'>🧸 Explain Like I'm 5</h1>", unsafe_allow_html=True)
|
17 |
+
st.markdown("<p style='text-align: center;'>Ask anything and I’ll explain it super simply 👶</p>", unsafe_allow_html=True)
|
18 |
+
|
19 |
+
# User input
|
20 |
+
user_input = st.text_input("🎯 Enter a topic or question:", placeholder="e.g., What is blockchain?")
|
21 |
+
|
22 |
+
with st.expander("💡 Try These Examples"):
|
23 |
+
st.markdown("- What is AI?\n- Why is the sky blue?\n- How does Wi-Fi work?\n- What is climate change?")
|
24 |
+
|
25 |
+
# ELI5 response
|
26 |
+
def generate_eli5_response(topic):
|
27 |
+
prompt = f"Explain like I'm 5: {topic}"
|
28 |
+
result = generator(prompt, max_new_tokens=200)
|
29 |
+
return result[0]['generated_text'].strip()
|
30 |
+
|
31 |
+
# PDF export
|
32 |
+
def export_to_pdf(topic, explanation):
|
33 |
+
pdf = FPDF()
|
34 |
+
pdf.add_page()
|
35 |
+
pdf.set_font("Arial", size=12)
|
36 |
+
pdf.multi_cell(0, 10, f"Topic: {topic}\n\nExplanation:\n{explanation}")
|
37 |
+
return pdf.output(dest='S').encode('latin-1')
|
38 |
+
|
39 |
+
# Button action
|
40 |
+
if st.button("✨ Explain it to me!"):
|
41 |
+
if user_input.strip() == "":
|
42 |
+
st.warning("Please enter a topic.")
|
43 |
+
else:
|
44 |
+
with st.spinner("Explaining like you're 5..."):
|
45 |
+
explanation = generate_eli5_response(user_input)
|
46 |
+
st.success("🍼 Here's your explanation:")
|
47 |
+
st.markdown(f"**{explanation}**")
|
48 |
+
|
49 |
+
# Export to PDF
|
50 |
+
pdf_data = export_to_pdf(user_input, explanation)
|
51 |
+
st.download_button("📄 Download as PDF", data=pdf_data, file_name=f"ELI5-{user_input[:30]}.pdf", mime="application/pdf")
|
52 |
+
|
53 |
+
# Footer
|
54 |
+
st.markdown("---")
|
55 |
+
st.markdown("<p style='text-align: center;'>❤️ Made with Love. By Akash Shahade</p>", unsafe_allow_html=True)
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
streamlit
|
3 |
+
transformers
|
4 |
+
torch
|
5 |
+
fpdf
|