Spaces:
Running
Running
siddhartharya
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -12,14 +12,25 @@ import re
|
|
12 |
import base64
|
13 |
import logging
|
14 |
import os
|
|
|
15 |
|
16 |
# Import OpenAI library
|
17 |
import openai
|
18 |
|
19 |
-
# Set up logging
|
20 |
-
logging.basicConfig(filename='app.log', level=logging.INFO,
|
21 |
-
format='%(asctime)s %(levelname)s %(name)s %(message)s')
|
22 |
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
# Initialize models and variables
|
25 |
logger.info("Initializing models and variables")
|
@@ -56,13 +67,13 @@ CATEGORIES = [
|
|
56 |
|
57 |
# Set up Groq Cloud API key and base URL
|
58 |
GROQ_API_KEY = os.getenv('GROQ_API_KEY')
|
|
|
59 |
if not GROQ_API_KEY:
|
60 |
logger.error("GROQ_API_KEY environment variable not set.")
|
61 |
-
raise ValueError("Please set the GROQ_API_KEY environment variable.")
|
62 |
|
63 |
# Set OpenAI API key and base URL to use Groq Cloud API
|
64 |
openai.api_key = GROQ_API_KEY
|
65 |
-
openai.api_base = "https://api.groq.com/openai/v1"
|
66 |
|
67 |
# Function to parse bookmarks from HTML
|
68 |
def parse_bookmarks(file_content):
|
@@ -307,6 +318,10 @@ def process_uploaded_file(file):
|
|
307 |
|
308 |
# Chatbot response using Groq Cloud API
|
309 |
def chatbot_response(user_query):
|
|
|
|
|
|
|
|
|
310 |
if not bookmarks:
|
311 |
logger.warning("No bookmarks available for chatbot")
|
312 |
return "No bookmarks available. Please upload and process your bookmarks first."
|
@@ -336,7 +351,7 @@ Please identify the most relevant bookmarks that match the user's query. Provide
|
|
336 |
|
337 |
# Call the Groq Cloud API via the OpenAI client
|
338 |
response = openai.ChatCompletion.create(
|
339 |
-
model='llama3-8b-8192', #
|
340 |
messages=[
|
341 |
{"role": "system", "content": "You help users find relevant bookmarks based on their queries."},
|
342 |
{"role": "user", "content": prompt}
|
@@ -351,8 +366,10 @@ Please identify the most relevant bookmarks that match the user's query. Provide
|
|
351 |
return answer
|
352 |
|
353 |
except Exception as e:
|
354 |
-
|
355 |
-
|
|
|
|
|
356 |
|
357 |
# Edit a bookmark
|
358 |
def edit_bookmark(bookmark_idx, new_title, new_url, new_category):
|
|
|
12 |
import base64
|
13 |
import logging
|
14 |
import os
|
15 |
+
import sys
|
16 |
|
17 |
# Import OpenAI library
|
18 |
import openai
|
19 |
|
20 |
+
# Set up logging to output to the console
|
|
|
|
|
21 |
logger = logging.getLogger(__name__)
|
22 |
+
logger.setLevel(logging.INFO)
|
23 |
+
|
24 |
+
# Create a console handler
|
25 |
+
console_handler = logging.StreamHandler(sys.stdout)
|
26 |
+
console_handler.setLevel(logging.INFO)
|
27 |
+
|
28 |
+
# Create a formatter and set it for the handler
|
29 |
+
formatter = logging.Formatter('%(asctime)s %(levelname)s %(name)s %(message)s')
|
30 |
+
console_handler.setFormatter(formatter)
|
31 |
+
|
32 |
+
# Add the handler to the logger
|
33 |
+
logger.addHandler(console_handler)
|
34 |
|
35 |
# Initialize models and variables
|
36 |
logger.info("Initializing models and variables")
|
|
|
67 |
|
68 |
# Set up Groq Cloud API key and base URL
|
69 |
GROQ_API_KEY = os.getenv('GROQ_API_KEY')
|
70 |
+
|
71 |
if not GROQ_API_KEY:
|
72 |
logger.error("GROQ_API_KEY environment variable not set.")
|
|
|
73 |
|
74 |
# Set OpenAI API key and base URL to use Groq Cloud API
|
75 |
openai.api_key = GROQ_API_KEY
|
76 |
+
openai.api_base = "https://api.groq.com/openai/v1/chat/completions" # Updated endpoint
|
77 |
|
78 |
# Function to parse bookmarks from HTML
|
79 |
def parse_bookmarks(file_content):
|
|
|
318 |
|
319 |
# Chatbot response using Groq Cloud API
|
320 |
def chatbot_response(user_query):
|
321 |
+
if not GROQ_API_KEY:
|
322 |
+
logger.warning("GROQ_API_KEY not set.")
|
323 |
+
return "API key not set. Please set the GROQ_API_KEY environment variable in the Hugging Face Space settings."
|
324 |
+
|
325 |
if not bookmarks:
|
326 |
logger.warning("No bookmarks available for chatbot")
|
327 |
return "No bookmarks available. Please upload and process your bookmarks first."
|
|
|
351 |
|
352 |
# Call the Groq Cloud API via the OpenAI client
|
353 |
response = openai.ChatCompletion.create(
|
354 |
+
model='llama3-8b-8192', # Verify this model name with Groq Cloud API documentation
|
355 |
messages=[
|
356 |
{"role": "system", "content": "You help users find relevant bookmarks based on their queries."},
|
357 |
{"role": "user", "content": prompt}
|
|
|
366 |
return answer
|
367 |
|
368 |
except Exception as e:
|
369 |
+
error_message = f"Error processing your query: {str(e)}"
|
370 |
+
logger.error(error_message)
|
371 |
+
print(error_message) # Ensure error appears in Hugging Face Spaces logs
|
372 |
+
return error_message
|
373 |
|
374 |
# Edit a bookmark
|
375 |
def edit_bookmark(bookmark_idx, new_title, new_url, new_category):
|