Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -97,8 +97,8 @@ class CreativeAgenticAI:
|
|
97 |
chutes_api_key: Chutes API key
|
98 |
model: Which model to use
|
99 |
"""
|
100 |
-
self.groq_api_key = groq_api_key
|
101 |
-
self.chutes_api_key = chutes_api_key
|
102 |
if not self.groq_api_key and model != "openai/gpt-oss-20b":
|
103 |
raise ValueError("No Groq API key provided")
|
104 |
if not self.chutes_api_key and model == "openai/gpt-oss-20b":
|
@@ -209,10 +209,10 @@ IMPORTANT: When you search the web and find information, you MUST:
|
|
209 |
|
210 |
# Add domain filtering for compound models (Groq only)
|
211 |
if self._supports_web_search():
|
212 |
-
if include_domains and include_domains[0].strip():
|
213 |
-
params["include_domains"] = [domain.strip() for domain in include_domains if domain.strip()]
|
214 |
-
if exclude_domains and exclude_domains[0].strip():
|
215 |
-
params["exclude_domains"] = [domain.strip() for domain in exclude_domains if domain.strip()]
|
216 |
|
217 |
# Add tools only for Groq models that support browser search
|
218 |
tools = []
|
@@ -239,7 +239,7 @@ IMPORTANT: When you search the web and find information, you MUST:
|
|
239 |
# Groq API call
|
240 |
params["max_completion_tokens"] = params.pop("max_tokens", None)
|
241 |
response = self.groq_client.chat.completions.create(**params)
|
242 |
-
content = response.choices[0].message.content
|
243 |
tool_calls = response.choices[0].message.tool_calls if hasattr(response.choices[0].message, "tool_calls") else None
|
244 |
|
245 |
# Extract tool usage information
|
@@ -306,12 +306,12 @@ IMPORTANT: When you search the web and find information, you MUST:
|
|
306 |
"tool_name": getattr(tool, "name", "unknown"),
|
307 |
}
|
308 |
if hasattr(tool, "input"):
|
309 |
-
tool_input = str(tool.input)
|
310 |
tool_dict["input"] = tool_input
|
311 |
if "search" in tool_dict["tool_name"].lower():
|
312 |
tool_info["search_queries"].append(tool_input)
|
313 |
if hasattr(tool, "output"):
|
314 |
-
tool_output = str(tool.output)
|
315 |
tool_dict["output"] = tool_output
|
316 |
urls = self._extract_urls(tool_output)
|
317 |
tool_info["sources_found"].extend(urls)
|
@@ -322,7 +322,7 @@ IMPORTANT: When you search the web and find information, you MUST:
|
|
322 |
for tool_call in tool_calls:
|
323 |
tool_dict = {
|
324 |
"tool_type": getattr(tool_call, "type", "browser_search"),
|
325 |
-
"tool_name": getattr(tool_call, "function", {}).get("name", "browser_search"),
|
326 |
"tool_id": getattr(tool_call, "id", None)
|
327 |
}
|
328 |
if hasattr(tool_call, "function") and hasattr(tool_call.function, "arguments"):
|
@@ -330,21 +330,25 @@ IMPORTANT: When you search the web and find information, you MUST:
|
|
330 |
args = json.loads(tool_call.function.arguments) if isinstance(tool_call.function.arguments, str) else tool_call.function.arguments
|
331 |
tool_dict["arguments"] = args
|
332 |
if "query" in args:
|
333 |
-
tool_info["search_queries"].append(args["query"])
|
334 |
except:
|
335 |
-
tool_dict["arguments"] = str(tool_call.function.arguments)
|
336 |
tool_info["tools_used"].append(tool_dict)
|
337 |
|
338 |
return tool_info
|
339 |
|
340 |
def _extract_urls(self, text: str) -> List[str]:
|
341 |
"""Extract URLs from text"""
|
|
|
|
|
342 |
url_pattern = r'https?://[^\s<>"]{2,}'
|
343 |
urls = re.findall(url_pattern, text)
|
344 |
return list(set(urls))
|
345 |
|
346 |
def _enhance_citations(self, content: str, tool_info: Dict) -> str:
|
347 |
"""Enhance content with better citation formatting"""
|
|
|
|
|
348 |
if not tool_info or not tool_info.get("sources_found"):
|
349 |
return content
|
350 |
|
@@ -359,6 +363,8 @@ IMPORTANT: When you search the web and find information, you MUST:
|
|
359 |
|
360 |
def _extract_domain(self, url: str) -> str:
|
361 |
"""Extract domain name from URL for display"""
|
|
|
|
|
362 |
try:
|
363 |
if url.startswith(('http://', 'https://')):
|
364 |
domain = url.split('/')[2]
|
@@ -395,6 +401,10 @@ async def validate_api_keys(groq_api_key: str, chutes_api_key: str, model: str)
|
|
395 |
"""Validate both Groq and Chutes API keys and initialize AI instance"""
|
396 |
global ai_instance, api_key_status
|
397 |
|
|
|
|
|
|
|
|
|
398 |
if model == "openai/gpt-oss-20b" and not chutes_api_key:
|
399 |
api_key_status = "Invalid ❌"
|
400 |
return "❌ Please enter a valid Chutes API key for the selected model"
|
@@ -490,11 +500,16 @@ async def chat_with_ai(message: str,
|
|
490 |
history.append([message, error_msg])
|
491 |
return history, ""
|
492 |
|
493 |
-
if not message.strip():
|
494 |
return history, ""
|
495 |
|
496 |
-
|
497 |
-
|
|
|
|
|
|
|
|
|
|
|
498 |
|
499 |
try:
|
500 |
response = await ai_instance.chat(
|
@@ -508,7 +523,7 @@ async def chat_with_ai(message: str,
|
|
508 |
force_search=force_search
|
509 |
)
|
510 |
|
511 |
-
ai_response = response["content"]
|
512 |
|
513 |
# Add tool usage info for Groq models
|
514 |
if response.get("tool_usage") and ai_instance.model != "openai/gpt-oss-20b":
|
@@ -971,7 +986,5 @@ def create_gradio_app():
|
|
971 |
if __name__ == "__main__":
|
972 |
app = create_gradio_app()
|
973 |
app.launch(
|
974 |
-
share=True
|
975 |
-
server_name="0.0.0.0",
|
976 |
-
server_port=7860
|
977 |
)
|
|
|
97 |
chutes_api_key: Chutes API key
|
98 |
model: Which model to use
|
99 |
"""
|
100 |
+
self.groq_api_key = groq_api_key or ""
|
101 |
+
self.chutes_api_key = chutes_api_key or ""
|
102 |
if not self.groq_api_key and model != "openai/gpt-oss-20b":
|
103 |
raise ValueError("No Groq API key provided")
|
104 |
if not self.chutes_api_key and model == "openai/gpt-oss-20b":
|
|
|
209 |
|
210 |
# Add domain filtering for compound models (Groq only)
|
211 |
if self._supports_web_search():
|
212 |
+
if include_domains and include_domains[0] and include_domains[0].strip():
|
213 |
+
params["include_domains"] = [domain.strip() for domain in include_domains if domain and domain.strip()]
|
214 |
+
if exclude_domains and exclude_domains[0] and exclude_domains[0].strip():
|
215 |
+
params["exclude_domains"] = [domain.strip() for domain in exclude_domains if domain and domain.strip()]
|
216 |
|
217 |
# Add tools only for Groq models that support browser search
|
218 |
tools = []
|
|
|
239 |
# Groq API call
|
240 |
params["max_completion_tokens"] = params.pop("max_tokens", None)
|
241 |
response = self.groq_client.chat.completions.create(**params)
|
242 |
+
content = response.choices[0].message.content or ""
|
243 |
tool_calls = response.choices[0].message.tool_calls if hasattr(response.choices[0].message, "tool_calls") else None
|
244 |
|
245 |
# Extract tool usage information
|
|
|
306 |
"tool_name": getattr(tool, "name", "unknown"),
|
307 |
}
|
308 |
if hasattr(tool, "input"):
|
309 |
+
tool_input = str(tool.input) if tool.input is not None else ""
|
310 |
tool_dict["input"] = tool_input
|
311 |
if "search" in tool_dict["tool_name"].lower():
|
312 |
tool_info["search_queries"].append(tool_input)
|
313 |
if hasattr(tool, "output"):
|
314 |
+
tool_output = str(tool.output) if tool.output is not None else ""
|
315 |
tool_dict["output"] = tool_output
|
316 |
urls = self._extract_urls(tool_output)
|
317 |
tool_info["sources_found"].extend(urls)
|
|
|
322 |
for tool_call in tool_calls:
|
323 |
tool_dict = {
|
324 |
"tool_type": getattr(tool_call, "type", "browser_search"),
|
325 |
+
"tool_name": getattr(tool_call, "function", {}).get("name", "browser_search") if hasattr(tool_call, "function") else "browser_search",
|
326 |
"tool_id": getattr(tool_call, "id", None)
|
327 |
}
|
328 |
if hasattr(tool_call, "function") and hasattr(tool_call.function, "arguments"):
|
|
|
330 |
args = json.loads(tool_call.function.arguments) if isinstance(tool_call.function.arguments, str) else tool_call.function.arguments
|
331 |
tool_dict["arguments"] = args
|
332 |
if "query" in args:
|
333 |
+
tool_info["search_queries"].append(str(args["query"]))
|
334 |
except:
|
335 |
+
tool_dict["arguments"] = str(tool_call.function.arguments) if tool_call.function.arguments is not None else ""
|
336 |
tool_info["tools_used"].append(tool_dict)
|
337 |
|
338 |
return tool_info
|
339 |
|
340 |
def _extract_urls(self, text: str) -> List[str]:
|
341 |
"""Extract URLs from text"""
|
342 |
+
if not text:
|
343 |
+
return []
|
344 |
url_pattern = r'https?://[^\s<>"]{2,}'
|
345 |
urls = re.findall(url_pattern, text)
|
346 |
return list(set(urls))
|
347 |
|
348 |
def _enhance_citations(self, content: str, tool_info: Dict) -> str:
|
349 |
"""Enhance content with better citation formatting"""
|
350 |
+
if not content:
|
351 |
+
return ""
|
352 |
if not tool_info or not tool_info.get("sources_found"):
|
353 |
return content
|
354 |
|
|
|
363 |
|
364 |
def _extract_domain(self, url: str) -> str:
|
365 |
"""Extract domain name from URL for display"""
|
366 |
+
if not url:
|
367 |
+
return ""
|
368 |
try:
|
369 |
if url.startswith(('http://', 'https://')):
|
370 |
domain = url.split('/')[2]
|
|
|
401 |
"""Validate both Groq and Chutes API keys and initialize AI instance"""
|
402 |
global ai_instance, api_key_status
|
403 |
|
404 |
+
# Handle None values
|
405 |
+
groq_api_key = groq_api_key or ""
|
406 |
+
chutes_api_key = chutes_api_key or ""
|
407 |
+
|
408 |
if model == "openai/gpt-oss-20b" and not chutes_api_key:
|
409 |
api_key_status = "Invalid ❌"
|
410 |
return "❌ Please enter a valid Chutes API key for the selected model"
|
|
|
500 |
history.append([message, error_msg])
|
501 |
return history, ""
|
502 |
|
503 |
+
if not message or not message.strip():
|
504 |
return history, ""
|
505 |
|
506 |
+
# Handle None values and empty strings
|
507 |
+
include_domains = include_domains or ""
|
508 |
+
exclude_domains = exclude_domains or ""
|
509 |
+
system_prompt = system_prompt or ""
|
510 |
+
|
511 |
+
include_list = [d.strip() for d in include_domains.split(",") if d.strip()] if include_domains.strip() else []
|
512 |
+
exclude_list = [d.strip() for d in exclude_domains.split(",") if d.strip()] if exclude_domains.strip() else []
|
513 |
|
514 |
try:
|
515 |
response = await ai_instance.chat(
|
|
|
523 |
force_search=force_search
|
524 |
)
|
525 |
|
526 |
+
ai_response = response["content"] or "No response received"
|
527 |
|
528 |
# Add tool usage info for Groq models
|
529 |
if response.get("tool_usage") and ai_instance.model != "openai/gpt-oss-20b":
|
|
|
986 |
if __name__ == "__main__":
|
987 |
app = create_gradio_app()
|
988 |
app.launch(
|
989 |
+
share=True
|
|
|
|
|
990 |
)
|