Mahrukhh commited on
Commit
9fc6125
Β·
verified Β·
1 Parent(s): 610bbbc

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +202 -0
app.py ADDED
@@ -0,0 +1,202 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+ import streamlit as st
3
+ import requests
4
+ from dotenv import load_dotenv
5
+ import os
6
+ from streamlit_lottie import st_lottie
7
+ import fitz # PyMuPDF
8
+ from docx import Document
9
+ import io
10
+
11
+ # Load environment variables
12
+ load_dotenv()
13
+ API_KEY = os.getenv("GROQ_API_KEY")
14
+
15
+ st.set_page_config(
16
+ page_title="AI Creativity Playground 🎨",
17
+ layout="centered",
18
+ page_icon="🎨"
19
+ )
20
+
21
+ def load_lottie_url(url):
22
+ try:
23
+ r = requests.get(url)
24
+ if r.status_code == 200:
25
+ return r.json()
26
+ except:
27
+ return None
28
+
29
+ ai_animation = load_lottie_url("https://lottie.host/67f1d10d-4d0d-4c99-a889-55b37e993b76/RSTX8aZ5u2.json")
30
+
31
+ st.markdown("<h1 style='text-align: center; color: #d63384;'>🌈 AI Creativity Playground</h1>", unsafe_allow_html=True)
32
+ if ai_animation:
33
+ st_lottie(ai_animation, height=250, speed=1)
34
+
35
+ def word_count_slider(label):
36
+ return st.slider(label, 50, 500, step=50, value=150)
37
+
38
+ def generate_text(prompt, word_limit):
39
+ headers = {
40
+ "Authorization": f"Bearer {API_KEY}",
41
+ "Content-Type": "application/json"
42
+ }
43
+ body = {
44
+ "model": "llama3-8b-8192",
45
+ "messages": [
46
+ {"role": "system", "content": "You are a creative assistant."},
47
+ {"role": "user", "content": f"{prompt}. Limit around {word_limit} words. End with a complete sentence."}
48
+ ],
49
+ "temperature": 0.9
50
+ }
51
+ try:
52
+ res = requests.post("https://api.groq.com/openai/v1/chat/completions", headers=headers, json=body)
53
+ res.raise_for_status()
54
+ return res.json()["choices"][0]["message"]["content"].strip()
55
+ except Exception as e:
56
+ return f"⚠ Error: {e}"
57
+
58
+ languages = ["English", "Urdu", "Spanish", "French", "Arabic", "Chinese", "German", "Hindi", "Turkish", "Japanese", "Russian", "Portuguese"]
59
+
60
+ tabs = st.tabs(["πŸ“– Story Generator", "πŸ“ Poem Creator", "πŸ˜‚ Emoji Story", "🎯 Slogan Generator", "πŸ“˜ Study Notes Generator", "🧹 Essay Improver"])
61
+
62
+ # --- STORY ---
63
+ with tabs[0]:
64
+ st.subheader("πŸ“– Create a Magical Story")
65
+ st.session_state.story_input = st.text_input("πŸ’‘ What's your story idea?", st.session_state.get("story_input", ""))
66
+ lang = st.selectbox("🌐 Select story language", languages, key="story_lang")
67
+ word_limit = word_count_slider("πŸ”’ Select word limit for story")
68
+ out = st.empty()
69
+ if st.button("✨ Generate Story", key="story_btn"):
70
+ if st.session_state.story_input:
71
+ result = generate_text(f"Write a creative story about: {st.session_state.story_input}. Write it in {lang}.", word_limit)
72
+ out.success("βœ… Story Generated:")
73
+ out.markdown(result.replace("\n", " \n"))
74
+ else:
75
+ out.warning("🚨 Please enter a story idea.")
76
+ if st.button("🧹 Clear Chat", key="clear_story"):
77
+ st.session_state.story_input = ""
78
+ out.empty()
79
+ st.rerun()
80
+
81
+ # --- POEM ---
82
+ with tabs[1]:
83
+ st.subheader("πŸ“ Compose a Beautiful Poem")
84
+ st.session_state.poem_input = st.text_input("🌸 Poem topic", st.session_state.get("poem_input", ""))
85
+ lang = st.selectbox("🌐 Select poem language", languages, key="poem_lang")
86
+ word_limit = word_count_slider("πŸ”’ Select word limit for poem")
87
+ out = st.empty()
88
+ if st.button("🧠 Generate Poem", key="poem_btn"):
89
+ if st.session_state.poem_input:
90
+ prompt = f"Write a poem on: {st.session_state.poem_input}. Format it in poetic verse β€” each line on a new line. Use 2–4 lines per stanza and separate stanzas with blank lines. Avoid writing it as a paragraph. Write it in {lang}."
91
+ result = generate_text(prompt, word_limit)
92
+ out.success("βœ… Poem Generated:")
93
+ out.markdown(result.replace("\n", " \n"))
94
+ else:
95
+ out.warning("🚨 Please enter a poem topic.")
96
+ if st.button("🧹 Clear Chat", key="clear_poem"):
97
+ st.session_state.poem_input = ""
98
+ out.empty()
99
+ st.rerun()
100
+
101
+ # --- EMOJI STORY ---
102
+ with tabs[2]:
103
+ st.subheader("πŸ˜‚ Tell a Story with Emojis")
104
+ st.session_state.emoji_input = st.text_input("🎭 Fun theme or scenario", st.session_state.get("emoji_input", ""))
105
+ lang = st.selectbox("🌐 Select emoji story language", languages, key="emoji_lang")
106
+ word_limit = word_count_slider("πŸ”’ Select word limit for emoji story")
107
+ out = st.empty()
108
+ if st.button("πŸ˜„ Generate Emoji Story", key="emoji_btn"):
109
+ if st.session_state.emoji_input:
110
+ result = generate_text(f"Write a funny and creative emoji story about: {st.session_state.emoji_input}. Write it in {lang}.", word_limit)
111
+ out.success("βœ… Emoji Story Generated:")
112
+ out.markdown(result.replace("\n", " \n"))
113
+ else:
114
+ out.warning("🚨 Please enter a fun theme.")
115
+ if st.button("🧹 Clear Chat", key="clear_emoji"):
116
+ st.session_state.emoji_input = ""
117
+ out.empty()
118
+ st.rerun()
119
+
120
+ # --- SLOGAN GENERATOR ---
121
+ with tabs[3]:
122
+ st.subheader("🎯 Generate a Catchy Slogan")
123
+ st.session_state.slogan_input = st.text_input("πŸ’¬ What's the product, brand, or concept?", st.session_state.get("slogan_input", ""))
124
+ lang = st.selectbox("🌐 Select slogan language", languages, key="slogan_lang")
125
+ word_limit = word_count_slider("πŸ”’ Select word limit for slogan")
126
+ out = st.empty()
127
+ if st.button("πŸš€ Generate Slogan", key="slogan_btn"):
128
+ if st.session_state.slogan_input:
129
+ result = generate_text(f"Create a creative and impactful slogan for: {st.session_state.slogan_input}. Write it in {lang}.", word_limit)
130
+ out.success("βœ… Slogan Generated:")
131
+ out.markdown(result.replace("\n", " \n"))
132
+ else:
133
+ out.warning("🚨 Please enter a slogan idea.")
134
+ if st.button("🧹 Clear Chat", key="clear_slogan"):
135
+ st.session_state.slogan_input = ""
136
+ out.empty()
137
+ st.rerun()
138
+
139
+ # --- STUDY NOTES GENERATOR ---
140
+ with tabs[4]:
141
+ st.subheader("πŸ“˜ Study Notes Generator")
142
+
143
+ input_mode = st.radio("πŸ“₯ Input Method", ["✍ Paste Text", "πŸ“‚ Upload File"], horizontal=True)
144
+ text_input = ""
145
+
146
+ if input_mode == "✍ Paste Text":
147
+ st.session_state.notes_input = st.text_area("πŸ“– Paste your study material", st.session_state.get("notes_input", ""))
148
+ text_input = st.session_state.notes_input
149
+ else:
150
+ uploaded_file = st.file_uploader("πŸ“‚ Upload a file (.txt, .pdf, .docx)", type=["txt", "pdf", "docx"])
151
+ if uploaded_file is not None:
152
+ if uploaded_file.name.endswith(".txt"):
153
+ text_input = uploaded_file.read().decode("utf-8")
154
+ elif uploaded_file.name.endswith(".pdf"):
155
+ with fitz.open(stream=uploaded_file.read(), filetype="pdf") as doc:
156
+ text_input = ""
157
+ for page in doc:
158
+ text_input += page.get_text()
159
+ elif uploaded_file.name.endswith(".docx"):
160
+ doc = Document(io.BytesIO(uploaded_file.read()))
161
+ text_input = "\n".join([para.text for para in doc.paragraphs])
162
+ else:
163
+ text_input = ""
164
+ st.warning("Unsupported file format.")
165
+ st.text_area("πŸ“– File Content", text_input, height=200, disabled=True)
166
+
167
+ mode = st.radio("✨ Choose mode", ["Simple", "Detailed", "Flashcards"], horizontal=True)
168
+ lang = st.selectbox("🌐 Select language", languages, key="notes_lang")
169
+ out = st.empty()
170
+
171
+ if st.button("🧠 Generate Notes", key="notes_btn"):
172
+ if text_input.strip():
173
+ prompt = f"Summarize this content in {mode.lower()} bullet points. Write in {lang}: {text_input}"
174
+ result = generate_text(prompt, 300)
175
+ out.success("βœ… Notes Generated:")
176
+ out.markdown(result.replace("\n", " \n"))
177
+ else:
178
+ out.warning("🚨 Please provide text input or upload a file.")
179
+
180
+ if st.button("🧹 Clear Chat", key="clear_notes"):
181
+ st.session_state.notes_input = ""
182
+ out.empty()
183
+ st.rerun()
184
+
185
+ # --- ESSAY IMPROVER ---
186
+ with tabs[5]:
187
+ st.subheader("🧹 Essay Improver")
188
+ st.session_state.essay_input = st.text_area("πŸ“ Paste your essay", st.session_state.get("essay_input", ""))
189
+ lang = st.selectbox("🌐 Select language", languages, key="essay_lang")
190
+ out = st.empty()
191
+ if st.button("πŸ’‘ Improve Essay", key="essay_btn"):
192
+ if st.session_state.essay_input:
193
+ prompt = f"Improve the grammar, clarity, and word choice of this essay. Show original and improved versions side by side. Write in {lang}: {st.session_state.essay_input}"
194
+ result = generate_text(prompt, 400)
195
+ out.success("βœ… Essay Improved:")
196
+ out.markdown(result.replace("\n", " \n"))
197
+ else:
198
+ out.warning("🚨 Please paste your essay.")
199
+ if st.button("🧹 Clear Chat", key="clear_essay"):
200
+ st.session_state.essay_input = ""
201
+ out.empty()
202
+ st.rerun()