Anupam007 commited on
Commit
2040bcf
·
verified ·
1 Parent(s): 5ccf719

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +289 -0
app.py ADDED
@@ -0,0 +1,289 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Step 1: Install/Update necessary libraries
2
+ # Using -U ensures the latest version of google-generativeai is installed
3
+ #!pip install -q -U gradio duckduckgo_search google-generativeai python-dotenv
4
+
5
+ # --- IMPORTANT: After this cell runs, RESTART THE RUNTIME ---
6
+ # --- (Runtime -> Restart runtime) for the library update to take effect ---
7
+
8
+ # Step 2: Import libraries
9
+ import gradio as gr
10
+ import google.generativeai as genai
11
+ from duckduckgo_search import DDGS
12
+ import os
13
+ import textwrap
14
+ from google.colab import userdata # For Colab secrets
15
+ import traceback # For detailed error logging if needed
16
+
17
+ # Step 3: Configure API Key (Using Colab Secrets)
18
+ # --- Instructions ---
19
+ # 1. Go to the "Secrets" tab (key icon 🔑) on the left pane in Colab.
20
+ # 2. Click "+ Add a new secret".
21
+ # 3. Set the NAME exactly as: GOOGLE_API_KEY
22
+ # 4. Paste your actual Google AI API key into the VALUE field.
23
+ # 5. Ensure the "Notebook access" toggle is ON for this secret.
24
+ # 6. Do NOT paste your API key directly into this code.
25
+
26
+ # Initialize flag and key variable
27
+ is_api_configured = False
28
+ GOOGLE_API_KEY = None
29
+
30
+ print("⚙️ Attempting to configure Google API Key...")
31
+ try:
32
+ GOOGLE_API_KEY = userdata.get('GOOGLE_API_KEY')
33
+
34
+ if GOOGLE_API_KEY:
35
+ genai.configure(api_key=GOOGLE_API_KEY)
36
+ print("✅ Google API Key configured successfully.")
37
+ is_api_configured = True # Set flag to True ONLY if configure() succeeds
38
+ else:
39
+ # Secret found but empty - treat as not configured
40
+ print("⚠️ Error: GOOGLE_API_KEY secret found but is empty.")
41
+ is_api_configured = False
42
+
43
+ except userdata.SecretNotFoundError:
44
+ print("❌ Error: Secret 'GOOGLE_API_KEY' not found.")
45
+ print("➡️ Please go to 'Secrets' (key icon 🔑) and add GOOGLE_API_KEY with your API key value.")
46
+ is_api_configured = False # Not configured if secret not found
47
+ except Exception as e:
48
+ print(f"❌ An unexpected error occurred during API Key configuration: {e}")
49
+ is_api_configured = False # Not configured if any other error occurs
50
+ # traceback.print_exc() # Uncomment for detailed traceback during configuration
51
+
52
+ # --- End of API Key Configuration ---
53
+
54
+
55
+ # Step 4: Define Helper Functions
56
+
57
+ # Function to perform web search
58
+ def search_web(query, num_results=7):
59
+ """Searches the web using DuckDuckGo and returns formatted results."""
60
+ print(f"🔍 Searching the web for: '{query}'...")
61
+ try:
62
+ with DDGS() as ddgs:
63
+ # Using region='wt-wt' for potentially broader results
64
+ results = list(ddgs.text(query, region='wt-wt', safesearch='off', max_results=num_results)) # Added safesearch='off'
65
+ if not results:
66
+ print("⚠️ No search results found.")
67
+ return "No relevant search results found for the query."
68
+
69
+ # Format results for the LLM
70
+ context = f"Search results for query '{query}':\n\n"
71
+ for i, result in enumerate(results):
72
+ context += f"Source [{i+1}]: {result.get('title', 'N/A')}\n"
73
+ context += f" URL: {result.get('href', 'N/A')}\n"
74
+ snippet = result.get('body', 'N/A')
75
+ context += f" Snippet: {snippet}\n\n"
76
+
77
+ print(f"✅ Found {len(results)} results.")
78
+ return context
79
+ except Exception as e:
80
+ print(f"❌ Error during web search: {e}")
81
+ # traceback.print_exc() # Uncomment for detailed traceback
82
+ return f"Error occurred during web search. Details: {e}"
83
+
84
+ # Function to generate the case study using Gemini
85
+ def generate_case_study(topic, search_context):
86
+ """Generates a case study using Gemini based on the topic and search context."""
87
+ print(f"🤖 Generating case study for: '{topic}'...")
88
+
89
+ # --- Check 1: API Configuration ---
90
+ if not is_api_configured:
91
+ print("❌ Cannot generate: Google API Key not configured successfully.")
92
+ return "Error: Google API Key not configured successfully. Please check Colab Secrets setup and restart runtime."
93
+
94
+ # --- Check 2: Search Results ---
95
+ if "Error occurred during web search" in search_context or "No relevant search results found" in search_context:
96
+ print(f"❌ Cannot generate: Problem with search results.")
97
+ return f"Cannot generate case study due to search issues:\n{search_context}"
98
+
99
+ # --- Configure the Gemini model ---
100
+ model_name = 'gemini-1.5-flash-latest' # Using the recommended modern model
101
+ # model_name = 'gemini-1.0-pro' # Alternative if flash causes issues
102
+ # model_name = 'gemini-pro' # Less likely to work based on previous error
103
+ try:
104
+ print(f" Using model: {model_name}")
105
+ model = genai.GenerativeModel(model_name)
106
+ except Exception as e:
107
+ print(f"❌ Error initializing GenerativeModel '{model_name}': {e}")
108
+ # traceback.print_exc()
109
+ # Try to list available models if initialization fails
110
+ try:
111
+ available_models = [m.name for m in genai.list_models() if 'generateContent' in m.supported_generation_methods]
112
+ print(f" Available models supporting generateContent: {available_models}")
113
+ if not available_models:
114
+ return f"Error setting up the AI model: {e}. Additionally, no compatible models were found via ListModels."
115
+ else:
116
+ # Suggest trying one of the listed models
117
+ suggested_model = next((m for m in available_models if 'flash' in m or 'pro' in m), available_models[0]) # Simple suggestion logic
118
+ return f"Error setting up the AI model '{model_name}': {e}. You could try using one of the available models like: '{suggested_model.split('/')[-1]}'"
119
+
120
+ except Exception as list_e:
121
+ print(f" Additionally failed to list available models: {list_e}")
122
+ return f"Error setting up the AI model '{model_name}': {e}. Failed to list alternatives."
123
+
124
+
125
+ # --- Define the Prompt ---
126
+ prompt = f"""
127
+ You are an expert business analyst and case study writer.
128
+ Your task is to generate a comprehensive case study based on the following topic: "{topic}"
129
+
130
+ Use the provided search results as your *only* source of information. Synthesize the information into a well-structured case study.
131
+
132
+ **Required Case Study Format:**
133
+
134
+ **1. Title:** Create a concise and informative title based on the topic and findings.
135
+ **2. Introduction/Executive Summary:** Briefly introduce the subject and the core topic (challenge, initiative). State the key outcome *mentioned in the sources*.
136
+ **3. The Company/Subject:** Provide background information *only from the search results*.
137
+ **4. The Challenge/Problem:** Describe the specific business issue mentioned in the sources.
138
+ **5. The Solution:** Detail the implemented solution *based only on the sources*. Describe technology use (like GenAI) if mentioned.
139
+ **6. Implementation/Process:** (Optional) Describe *only if information is available in the sources*.
140
+ **7. Results/Impact:** Quantify results and impact using data *from the sources*. Describe qualitative benefits mentioned. If no results are mentioned, state that.
141
+ **8. Conclusion:** Summarize key takeaways *based on the provided information*. Reiterate challenge, solution, outcomes *from sources*.
142
+ **9. Sources:** List the URLs (`URL:` lines) from the search results that were most relevant.
143
+
144
+ **Instructions:**
145
+ * Adhere strictly to the format above. Use Markdown `##` for section headings (e.g., `## 1. Title`).
146
+ * Base your writing ***exclusively*** on the information in the "Provided Search Context". Do not invent information or use external knowledge.
147
+ * If details for a section are missing in the sources, explicitly state: "Information not available in the provided sources." Do not leave sections blank.
148
+ * Maintain an objective and professional tone.
149
+ * Ensure coherence and logical flow.
150
+ * Format the output using Markdown.
151
+
152
+ **Provided Search Context:**
153
+ ---
154
+ {search_context}
155
+ ---
156
+
157
+ Now, please generate the case study for "{topic}".
158
+ """
159
+
160
+ # --- Generate Content ---
161
+ try:
162
+ # Optional: Add safety settings if needed
163
+ # safety_settings = [...]
164
+ # response = model.generate_content(prompt, safety_settings=safety_settings)
165
+ response = model.generate_content(prompt)
166
+
167
+ # --- Process Response Safely ---
168
+ # Check for content parts first
169
+ if response.parts:
170
+ generated_text = "".join(part.text for part in response.parts)
171
+ print("✅ Case study generated successfully.")
172
+ return generated_text
173
+ # Check for blocking reasons if no parts exist
174
+ elif response.prompt_feedback and response.prompt_feedback.block_reason:
175
+ block_reason = response.prompt_feedback.block_reason
176
+ safety_info = f" Ratings: {response.prompt_feedback.safety_ratings}" if response.prompt_feedback.safety_ratings else ""
177
+ print(f"⚠️ Generation blocked due to: {block_reason}")
178
+ return f"Error: Generation failed. Blocked due to '{block_reason}'.{safety_info} Please try refining your topic or check content policies."
179
+ # Handle candidates being empty or other unexpected scenarios
180
+ elif not response.candidates:
181
+ finish_reason = response.candidates[0].finish_reason if response.candidates else "UNKNOWN" # Attempt to get finish reason
182
+ print(f"⚠️ Warning: Generation finished without valid content (Finish Reason: {finish_reason}).")
183
+ return f"Error: The AI model finished generation but produced no usable content (Reason: {finish_reason}). This might indicate an issue with the prompt, the model, or safety filters."
184
+ else:
185
+ # Fallback for other unexpected empty response scenarios
186
+ print("⚠️ Warning: Generation finished but produced no text content for unknown reasons.")
187
+ # You could inspect the raw response object here if needed: print(response)
188
+ return "Error: The AI model generated an empty response. This might be due to the input, content filters, or a temporary issue."
189
+
190
+
191
+ except Exception as e:
192
+ print(f"❌ Error during case study generation: {e}")
193
+ # traceback.print_exc() # Uncomment for detailed traceback
194
+ error_message = f"An unexpected error occurred during AI generation: {e}"
195
+ # Check for common API errors
196
+ if "API key not valid" in str(e) or "PermissionDenied" in str(e) or "AuthenticationError" in str(e):
197
+ error_message = "Error: Invalid, expired, or missing API Key. Please verify your GOOGLE_API_KEY secret in Colab and ensure the Gemini API is enabled in your Google Cloud project."
198
+ elif "Model not found" in str(e) or "models/" in str(e) and "is not found" in str(e):
199
+ # Make error more specific based on the original error message
200
+ error_message = f"Error: The specified AI model ('{model_name}') was not found or is not supported for generateContent with the current API version. Check the model name or try updating the library (`!pip install -U google-generativeai` + restart runtime)."
201
+ # Attempt to list models again here might be useful
202
+ try:
203
+ available_models = [m.name for m in genai.list_models() if 'generateContent' in m.supported_generation_methods]
204
+ error_message += f"\n Available models supporting generateContent: {available_models}"
205
+ except Exception:
206
+ error_message += "\n Failed to retrieve list of available models."
207
+
208
+ elif "Resource has been exhausted" in str(e) or "Quota" in str(e):
209
+ error_message = "Error: API quota exceeded. Please check your usage limits in Google Cloud Console or wait and try again."
210
+ elif "Invalid API key" in str(e):
211
+ error_message = "Error: The provided API key is invalid. Please double-check the GOOGLE_API_KEY secret value."
212
+ elif hasattr(e, 'message'): # General Google API error message structure
213
+ # Append original message if available and different
214
+ if str(e) != e.message:
215
+ error_message = f"Error during AI generation: {e.message} (Details: {e})"
216
+ else:
217
+ error_message = f"Error during AI generation: {e.message}"
218
+
219
+
220
+ return error_message
221
+
222
+
223
+ # Step 5: Define the main processing function for Gradio
224
+ def create_case_study(company_or_topic):
225
+ """Orchestrates the web search and case study generation process."""
226
+ print("-" * 60) # Separator for new request
227
+ if not company_or_topic or not company_or_topic.strip():
228
+ print("⚠️ Input validation failed: Empty topic received.")
229
+ return "Please enter a valid company name or topic."
230
+
231
+ cleaned_topic = company_or_topic.strip()
232
+ print(f"➡️ Processing request for: '{cleaned_topic}'")
233
+
234
+ # 1. Search the web
235
+ search_results_context = search_web(cleaned_topic)
236
+
237
+ # 2. Generate the case study (handles API key/search errors internally)
238
+ case_study_markdown = generate_case_study(cleaned_topic, search_results_context)
239
+
240
+ print("-" * 60) # Separator for end of request
241
+ return case_study_markdown
242
+
243
+ # Step 6: Create and Launch the Gradio Interface
244
+ print("\n⚙️ Setting up Gradio interface...")
245
+
246
+ # Add a final check before launching Gradio
247
+ if not is_api_configured:
248
+ print("\n" + "="*60)
249
+ print("‼️ WARNING: Google API Key not configured successfully. ‼️")
250
+ print(" The Gradio interface will launch, but case study generation WILL FAIL.")
251
+ print(" Please ensure the 'GOOGLE_API_KEY' secret is correctly set up in Colab")
252
+ print(" and that you have restarted the runtime after any library updates.")
253
+ print("="*60 + "\n")
254
+ # Optionally, prevent launch entirely:
255
+ # print("\n❌ ERROR: Google API Key not configured. Cannot launch Gradio interface.")
256
+ # exit() # Uncomment to stop execution
257
+
258
+ iface = gr.Interface(
259
+ fn=create_case_study,
260
+ inputs=gr.Textbox(
261
+ lines=2,
262
+ placeholder="Enter a company name or topic (e.g., 'Acme Corp uses AI for customer support' or 'History of Netflix recommendation engine')",
263
+ label="Company Name or Topic"
264
+ ),
265
+ outputs=gr.Markdown( # Use Markdown output for better formatting
266
+ label="Generated Case Study",
267
+ # line_breaks=True # Uncomment if you prefer single newlines to create <br> tags
268
+ ),
269
+ title="📄 AI Case Study Generator (Gemini + DuckDuckGo)",
270
+ description="Enter a company/topic. The app searches the web (DuckDuckGo) and uses Google's Gemini AI to write a case study *based only on the search results*. \n**Requires a correctly configured `GOOGLE_API_KEY` in Colab Secrets (🔑) and runtime restart after library updates.**",
271
+ allow_flagging="never",
272
+ examples=[
273
+ ["How Spotify uses AI for music recommendations"],
274
+ ["Tesla Autopilot development challenges"],
275
+ ["Use of AI in drug discovery by Pfizer"],
276
+ ["Environmental impact reduction using AI at Google data centers"],
277
+ ],
278
+ theme=gr.themes.Soft() # Optional: Apply a soft theme
279
+ )
280
+
281
+ print("🚀 Launching Gradio interface...")
282
+ # Launch in debug mode for detailed logs, share=True creates a public link (useful in Colab)
283
+ # Set share=False if you don't need a public link
284
+ try:
285
+ iface.launch(debug=True, share=True)
286
+ except Exception as e:
287
+ print(f"❌ Failed to launch Gradio interface: {e}")
288
+ print(" This might be due to networking issues, Colab limitations, or conflicts. Check logs.")
289
+ # traceback.print_exc() # Uncomment for detailed traceback on launch failure