Nahiyan14 commited on
Commit
2568443
·
verified ·
1 Parent(s): cbd5a41

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -9
app.py CHANGED
@@ -1,4 +1,5 @@
1
  import os
 
2
 
3
  # Try using a different directory path where you should have permissions
4
  os.environ['TRANSFORMERS_CACHE'] = '/tmp/model_cache'
@@ -15,17 +16,42 @@ from langchain_core.prompts import ChatPromptTemplate
15
  from dotenv import load_dotenv
16
  from src.prompt import *
17
 
18
-
19
  app = Flask(__name__)
20
 
21
  # Load environment variables - these will be set in Hugging Face Space secrets
22
  load_dotenv() # Still useful for local development
23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  PINECONE_API_KEY = os.environ.get('PINECONE_API_KEY')
25
  OPENAI_API_KEY = os.environ.get('OPENAI_API_KEY')
26
 
 
 
 
 
 
27
  if not PINECONE_API_KEY or not OPENAI_API_KEY:
28
- raise ValueError("Missing PINECONE_API_KEY or OPENAI_API_KEY")
 
29
 
30
  os.environ["PINECONE_API_KEY"] = PINECONE_API_KEY
31
  os.environ["OPENAI_API_KEY"] = OPENAI_API_KEY
@@ -37,18 +63,39 @@ rag_chain = None
37
  def initialize_chain():
38
  global embeddings, rag_chain
39
  try:
 
40
  embeddings = download_hugging_face_embeddings()
 
 
41
  index_name = "medprep"
 
 
 
 
 
 
 
 
 
 
 
 
 
42
 
43
- # Embed each chunk and upsert the embeddings into your Pinecone index.
44
  docsearch = Pinecone.from_existing_index(
45
- index_name=index_name,
46
- embedding=embeddings
47
  )
 
48
 
49
  retriever = docsearch.as_retriever(search_type="similarity", search_kwargs={"k":3})
 
50
 
 
51
  llm = OpenAI(temperature=0.4, max_tokens=500)
 
 
 
52
  prompt = ChatPromptTemplate.from_messages(
53
  [
54
  ("system", system_prompt),
@@ -56,16 +103,23 @@ def initialize_chain():
56
  ]
57
  )
58
 
 
59
  question_answer_chain = create_stuff_documents_chain(llm, prompt)
 
 
60
  rag_chain = create_retrieval_chain(retriever, question_answer_chain)
61
- print("RAG chain initialized successfully")
62
  return True
63
  except Exception as e:
64
  print(f"Failed to initialize RAG chain: {e}")
 
 
65
  return False
66
 
67
  # Initialize the chain when the application starts
68
- initialize_chain()
 
 
69
 
70
  @app.route("/")
71
  def index():
@@ -77,21 +131,31 @@ def chat():
77
 
78
  # Make sure chain is initialized
79
  if rag_chain is None:
 
80
  if not initialize_chain():
81
  return "Error: System not initialized properly. Please check the logs."
82
 
83
  msg = request.form["msg"]
84
  try:
 
85
  response = rag_chain.invoke({"input": msg})
 
86
  return str(response["answer"])
87
  except Exception as e:
88
- print(f"Error processing request: {e}")
 
 
89
  return f"Error: {str(e)}"
90
 
91
  # Health check endpoint for monitoring
92
  @app.route("/health")
93
  def health_check():
94
- return jsonify({"status": "healthy"})
 
 
 
 
 
95
 
96
  if __name__ == '__main__':
97
  port = int(os.environ.get("PORT", 7860))
 
1
  import os
2
+ import traceback
3
 
4
  # Try using a different directory path where you should have permissions
5
  os.environ['TRANSFORMERS_CACHE'] = '/tmp/model_cache'
 
16
  from dotenv import load_dotenv
17
  from src.prompt import *
18
 
 
19
  app = Flask(__name__)
20
 
21
  # Load environment variables - these will be set in Hugging Face Space secrets
22
  load_dotenv() # Still useful for local development
23
 
24
+ print("Starting application initialization")
25
+ print(f"Python version: {os.sys.version}")
26
+
27
+ # Add debugging endpoints
28
+ @app.route("/test")
29
+ def test():
30
+ return "Flask app is working. This is a test endpoint."
31
+
32
+ @app.route("/check-env")
33
+ def check_env():
34
+ has_pinecone = "Yes" if os.environ.get("PINECONE_API_KEY") else "No"
35
+ has_openai = "Yes" if os.environ.get("OPENAI_API_KEY") else "No"
36
+
37
+ # Check if keys appear valid (without revealing them)
38
+ pinecone_valid = len(os.environ.get("PINECONE_API_KEY", "")) > 10 if has_pinecone == "Yes" else "N/A"
39
+ openai_valid = os.environ.get("OPENAI_API_KEY", "").startswith("sk-") if has_openai == "Yes" else "N/A"
40
+
41
+ return f"Pinecone key present: {has_pinecone} (appears valid: {pinecone_valid})<br>OpenAI key present: {has_openai} (appears valid: {openai_valid})"
42
+
43
+ print("Checking environment variables...")
44
  PINECONE_API_KEY = os.environ.get('PINECONE_API_KEY')
45
  OPENAI_API_KEY = os.environ.get('OPENAI_API_KEY')
46
 
47
+ if not PINECONE_API_KEY:
48
+ print("WARNING: Missing PINECONE_API_KEY")
49
+ if not OPENAI_API_KEY:
50
+ print("WARNING: Missing OPENAI_API_KEY")
51
+
52
  if not PINECONE_API_KEY or not OPENAI_API_KEY:
53
+ print("CRITICAL ERROR: Missing API keys")
54
+ # We'll continue anyway to allow debugging, but the app won't work properly
55
 
56
  os.environ["PINECONE_API_KEY"] = PINECONE_API_KEY
57
  os.environ["OPENAI_API_KEY"] = OPENAI_API_KEY
 
63
  def initialize_chain():
64
  global embeddings, rag_chain
65
  try:
66
+ print("Step 1: Starting to download embeddings")
67
  embeddings = download_hugging_face_embeddings()
68
+ print("Step 2: Successfully downloaded embeddings")
69
+
70
  index_name = "medprep"
71
+ print(f"Step 3: Connecting to Pinecone index: {index_name}")
72
+
73
+ try:
74
+ from pinecone import Pinecone as PineconeClient
75
+ pc = PineconeClient(api_key=PINECONE_API_KEY)
76
+ # List available indexes to verify connection
77
+ indexes = pc.list_indexes()
78
+ print(f"Available Pinecone indexes: {indexes}")
79
+
80
+ if index_name not in [idx.name for idx in indexes]:
81
+ print(f"WARNING: Index '{index_name}' not found in your Pinecone account!")
82
+ except Exception as e:
83
+ print(f"Failed to connect to Pinecone API: {e}")
84
 
 
85
  docsearch = Pinecone.from_existing_index(
86
+ index_name=index_name,
87
+ embedding=embeddings
88
  )
89
+ print("Step 4: Successfully connected to Pinecone")
90
 
91
  retriever = docsearch.as_retriever(search_type="similarity", search_kwargs={"k":3})
92
+ print("Step 5: Created retriever")
93
 
94
+ print("Step 6: Initializing OpenAI")
95
  llm = OpenAI(temperature=0.4, max_tokens=500)
96
+ print("Step 7: OpenAI initialized")
97
+
98
+ print("Step 8: Creating prompt template")
99
  prompt = ChatPromptTemplate.from_messages(
100
  [
101
  ("system", system_prompt),
 
103
  ]
104
  )
105
 
106
+ print("Step 9: Creating QA chain")
107
  question_answer_chain = create_stuff_documents_chain(llm, prompt)
108
+
109
+ print("Step 10: Creating RAG chain")
110
  rag_chain = create_retrieval_chain(retriever, question_answer_chain)
111
+ print("Step 11: RAG chain initialized successfully")
112
  return True
113
  except Exception as e:
114
  print(f"Failed to initialize RAG chain: {e}")
115
+ print(f"Error type: {type(e)}")
116
+ traceback.print_exc()
117
  return False
118
 
119
  # Initialize the chain when the application starts
120
+ print("Starting chain initialization...")
121
+ initialization_result = initialize_chain()
122
+ print(f"Chain initialization result: {initialization_result}")
123
 
124
  @app.route("/")
125
  def index():
 
131
 
132
  # Make sure chain is initialized
133
  if rag_chain is None:
134
+ print("RAG chain not initialized, attempting to initialize again...")
135
  if not initialize_chain():
136
  return "Error: System not initialized properly. Please check the logs."
137
 
138
  msg = request.form["msg"]
139
  try:
140
+ print(f"Processing message: {msg[:30]}...") # Log only first 30 chars for privacy
141
  response = rag_chain.invoke({"input": msg})
142
+ print("Successfully generated response")
143
  return str(response["answer"])
144
  except Exception as e:
145
+ error_msg = f"Error processing request: {e}"
146
+ print(error_msg)
147
+ traceback.print_exc()
148
  return f"Error: {str(e)}"
149
 
150
  # Health check endpoint for monitoring
151
  @app.route("/health")
152
  def health_check():
153
+ is_initialized = rag_chain is not None
154
+ return jsonify({
155
+ "status": "healthy",
156
+ "rag_chain_initialized": is_initialized,
157
+ "embeddings_loaded": embeddings is not None
158
+ })
159
 
160
  if __name__ == '__main__':
161
  port = int(os.environ.get("PORT", 7860))