akashshahade commited on
Commit
794add9
·
verified ·
1 Parent(s): cdd7c16

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +115 -0
  2. requirements.txt +7 -0
app.py ADDED
@@ -0,0 +1,115 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import streamlit as st
3
+ from transformers import pipeline
4
+ from fpdf import FPDF
5
+ from googletrans import Translator
6
+ from gtts import gTTS
7
+ import base64
8
+ import tempfile
9
+
10
+ # Load Hugging Face model
11
+ @st.cache_resource
12
+ def load_model():
13
+ return pipeline("text-generation", model="mistralai/Mistral-7B-Instruct-v0.1")
14
+
15
+ generator = load_model()
16
+
17
+ # Translator
18
+ translator = Translator()
19
+
20
+ # Page config
21
+ st.set_page_config(page_title="Explain Like I'm 5", page_icon="🧸", layout="centered")
22
+ st.markdown("<h1 style='text-align: center;'>🧸 Explain Like I'm 5</h1>", unsafe_allow_html=True)
23
+ st.markdown("<p style='text-align: center;'>Ask anything and I’ll explain it super simply 👶</p>", unsafe_allow_html=True)
24
+
25
+ # Input
26
+ user_input = st.text_input("🎯 Enter a topic or question:", placeholder="e.g., What is blockchain?")
27
+ language = st.selectbox("🌐 Choose output language:", ["English", "Hindi", "Marathi"])
28
+
29
+ with st.expander("💡 Try These Examples"):
30
+ st.markdown("- What is AI?\n- Why is the sky blue?\n- How does Wi-Fi work?\n- What is climate change?")
31
+
32
+ # Hugging Face response
33
+ def generate_eli5_response(topic):
34
+ prompt = f"Explain this to a 5-year-old: {topic}"
35
+ result = generator(prompt, max_new_tokens=150, do_sample=True, temperature=0.7)
36
+ return result[0]['generated_text'].replace(prompt, "").strip()
37
+
38
+ # Translate
39
+ def translate_text(text, lang_code):
40
+ return translator.translate(text, dest=lang_code).text
41
+
42
+ # Language map
43
+ lang_map = {
44
+ "English": "en",
45
+ "Hindi": "hi",
46
+ "Marathi": "mr"
47
+ }
48
+
49
+ # PDF Export
50
+ def export_to_pdf(topic, explanation):
51
+ pdf = FPDF()
52
+ pdf.add_page()
53
+ pdf.set_font("Arial", size=12)
54
+ pdf.multi_cell(0, 10, f"Topic: {topic}\n\nExplanation:\n{explanation}")
55
+ return pdf.output(dest='S').encode('latin-1')
56
+
57
+ # Text-to-Speech
58
+ def text_to_speech(text, lang_code):
59
+ tts = gTTS(text, lang=lang_code)
60
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp:
61
+ tts.save(tmp.name)
62
+ audio_path = tmp.name
63
+ return audio_path
64
+
65
+ # History
66
+ if 'history' not in st.session_state:
67
+ st.session_state['history'] = []
68
+
69
+ # Button logic
70
+ if st.button("✨ Explain it to me!"):
71
+ if user_input.strip() == "":
72
+ st.warning("Please enter a topic.")
73
+ else:
74
+ with st.spinner("Explaining like you're 5..."):
75
+ explanation = generate_eli5_response(user_input)
76
+
77
+ # Translate if needed
78
+ lang_code = lang_map[language]
79
+ if language != "English":
80
+ explanation_translated = translate_text(explanation, lang_code)
81
+ else:
82
+ explanation_translated = explanation
83
+
84
+ # Save to history
85
+ st.session_state['history'].insert(0, {
86
+ "topic": user_input,
87
+ "language": language,
88
+ "explanation": explanation_translated
89
+ })
90
+ st.session_state['history'] = st.session_state['history'][:5]
91
+
92
+ # Display result
93
+ st.success("🍼 Here's your explanation:")
94
+ st.markdown(f"**{explanation_translated}**")
95
+
96
+ # TTS playback
97
+ audio_path = text_to_speech(explanation_translated, lang_code)
98
+ with open(audio_path, "rb") as audio_file:
99
+ audio_bytes = audio_file.read()
100
+ st.audio(audio_bytes, format="audio/mp3")
101
+
102
+ # Export to PDF
103
+ pdf_data = export_to_pdf(user_input, explanation_translated)
104
+ st.download_button("📄 Download as PDF", data=pdf_data, file_name=f"ELI5-{user_input[:30]}.pdf", mime="application/pdf")
105
+
106
+ # Show history
107
+ if st.session_state['history']:
108
+ with st.expander("📜 Past Explanations"):
109
+ for i, entry in enumerate(st.session_state['history']):
110
+ st.markdown(f"**{i+1}. {entry['topic']} ({entry['language']})**")
111
+ st.markdown(f"> {entry['explanation']}")
112
+
113
+ # Footer
114
+ st.markdown("---")
115
+ st.markdown("<p style='text-align: center;'>❤️ Made with Love. By Akash Shahade</p>", unsafe_allow_html=True)
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+
2
+ streamlit
3
+ transformers
4
+ torch
5
+ fpdf
6
+ gtts
7
+ googletrans==4.0.0-rc1