akashshahade commited on
Commit
e7994a3
·
verified ·
1 Parent(s): bb2e8ea

Upload 2 files

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