mroccuper commited on
Commit
979ed67
·
verified ·
1 Parent(s): 74483a8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -8
app.py CHANGED
@@ -15,6 +15,9 @@ def generate_seo_plan(topic, api_key, temperature=0.7, max_tokens=4000):
15
 
16
  if not api_key.strip():
17
  return "⚠️ Please provide your Gemini API key."
 
 
 
18
 
19
  try:
20
  # Configure Gemini with the provided API key
@@ -30,23 +33,34 @@ def generate_seo_plan(topic, api_key, temperature=0.7, max_tokens=4000):
30
  }
31
  )
32
 
 
 
 
33
  # Build the prompt
34
  prompt = build_prompt(topic)
35
 
 
 
 
36
  # Generate response
37
  response = model.generate_content(prompt)
38
 
 
 
 
 
39
  # Return formatted response
40
- return response.text
41
 
42
  except Exception as e:
43
  error_message = str(e)
 
44
  if "API key not valid" in error_message or "authentication" in error_message.lower():
45
- return "⚠️ API Key Error: The provided Gemini API key appears to be invalid. Please check your key and try again."
46
  elif "quota" in error_message.lower():
47
- return "⚠️ Quota Exceeded: Your Gemini API quota has been exceeded. Please check your Google AI Studio dashboard."
48
  else:
49
- return f"⚠️ Error: {error_message}"
50
 
51
  # Build enhanced SEO prompt for fermentation blog
52
  def build_prompt(topic):
@@ -120,7 +134,37 @@ custom_theme = gr.themes.Soft(
120
  )
121
 
122
  # Main Gradio app
123
- with gr.Blocks(title="Fermentation SEO Assistant", theme=custom_theme) as app:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
124
  # Header
125
  gr.Markdown("""
126
  # 🌱 Fermentation SEO Content Planner
@@ -190,14 +234,17 @@ with gr.Blocks(title="Fermentation SEO Assistant", theme=custom_theme) as app:
190
  # Output section with a visually appealing container
191
  with gr.Group():
192
  output_header = gr.Markdown("### Your SEO Plan Will Appear Here")
193
- seo_output = gr.Markdown()
 
 
194
 
195
  # Button actions
196
  generate_btn.click(
197
  fn=generate_seo_plan,
198
  inputs=[topic_input, api_key_input, temperature, max_tokens],
199
  outputs=seo_output,
200
- api_name="generate"
 
201
  )
202
 
203
  clear_btn.click(
@@ -220,4 +267,20 @@ with gr.Blocks(title="Fermentation SEO Assistant", theme=custom_theme) as app:
220
 
221
  # Launch the app
222
  if __name__ == "__main__":
223
- app.launch(share=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
  if not api_key.strip():
17
  return "⚠️ Please provide your Gemini API key."
18
+
19
+ # Show that processing has started
20
+ yield "🔄 Initializing and configuring Gemini API..."
21
 
22
  try:
23
  # Configure Gemini with the provided API key
 
33
  }
34
  )
35
 
36
+ # Update status
37
+ yield "🧠 Building SEO prompt for your fermentation topic..."
38
+
39
  # Build the prompt
40
  prompt = build_prompt(topic)
41
 
42
+ # Update status
43
+ yield "🚀 Generating SEO plan with Gemini 1.5 Pro..."
44
+
45
  # Generate response
46
  response = model.generate_content(prompt)
47
 
48
+ # Update status
49
+ yield "✨ Formatting your SEO plan..."
50
+ time.sleep(0.5) # Brief pause for UI feedback
51
+
52
  # Return formatted response
53
+ yield response.text
54
 
55
  except Exception as e:
56
  error_message = str(e)
57
+ print(f"Error generating SEO plan: {error_message}") # Debug output
58
  if "API key not valid" in error_message or "authentication" in error_message.lower():
59
+ yield "⚠️ API Key Error: The provided Gemini API key appears to be invalid. Please check your key and try again."
60
  elif "quota" in error_message.lower():
61
+ yield "⚠️ Quota Exceeded: Your Gemini API quota has been exceeded. Please check your Google AI Studio dashboard."
62
  else:
63
+ yield f"⚠️ Error: {error_message}"
64
 
65
  # Build enhanced SEO prompt for fermentation blog
66
  def build_prompt(topic):
 
134
  )
135
 
136
  # Main Gradio app
137
+ with gr.Blocks(title="Fermentation SEO Assistant", theme=custom_theme, css="""
138
+ #seo-output-container {
139
+ min-height: 300px;
140
+ max-height: 800px;
141
+ overflow-y: auto;
142
+ border-left: 4px solid #84cc16;
143
+ padding-left: 15px;
144
+ background-color: #f8fafc;
145
+ border-radius: 6px;
146
+ transition: all 0.3s ease;
147
+ }
148
+
149
+ #seo-output-container:empty {
150
+ border-left-color: #e5e7eb;
151
+ background-color: #ffffff;
152
+ }
153
+
154
+ .loading-spinner {
155
+ display: inline-block;
156
+ width: 50px;
157
+ height: 50px;
158
+ border: 3px solid rgba(0,0,0,.3);
159
+ border-radius: 50%;
160
+ border-top-color: #84cc16;
161
+ animation: spin 1s ease-in-out infinite;
162
+ }
163
+
164
+ @keyframes spin {
165
+ to { transform: rotate(360deg); }
166
+ }
167
+ """) as app:
168
  # Header
169
  gr.Markdown("""
170
  # 🌱 Fermentation SEO Content Planner
 
234
  # Output section with a visually appealing container
235
  with gr.Group():
236
  output_header = gr.Markdown("### Your SEO Plan Will Appear Here")
237
+ # Add progress indicator
238
+ progress_bar = gr.Progress(track_tqdm=True)
239
+ seo_output = gr.Markdown(elem_id="seo-output-container")
240
 
241
  # Button actions
242
  generate_btn.click(
243
  fn=generate_seo_plan,
244
  inputs=[topic_input, api_key_input, temperature, max_tokens],
245
  outputs=seo_output,
246
+ api_name="generate",
247
+ show_progress="full" # Add loading indicator
248
  )
249
 
250
  clear_btn.click(
 
267
 
268
  # Launch the app
269
  if __name__ == "__main__":
270
+ # Print some helpful debug info
271
+ print("Starting Fermentation SEO Assistant...")
272
+ print("Make sure you have installed all required packages:")
273
+ print(" - gradio")
274
+ print(" - google-generativeai")
275
+ print(" - python-dotenv")
276
+ print("\nIf you encounter issues, run: pip install -r requirements.txt")
277
+
278
+ # Launch with more informative messages
279
+ app.launch(
280
+ share=True,
281
+ debug=True,
282
+ show_error=True,
283
+ server_name="0.0.0.0", # Allow external connections
284
+ server_port=7860,
285
+ quiet=False # Show full logs
286
+ )