roshnn24 commited on
Commit
b545a36
·
verified ·
1 Parent(s): b936fed

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -28
app.py CHANGED
@@ -142,28 +142,22 @@ def init_db():
142
  conn.commit()
143
 
144
  def initialize_llm():
145
- """Initialize the model in the simplest way possible"""
146
  try:
147
- # Get API token
148
  api_token = os.environ.get('HF_TOKEN')
149
  if not api_token:
150
  raise ValueError("No API token found")
151
 
152
- # Initialize with minimal settings
153
- llm = HuggingFaceHub(
154
- repo_id="mistralai/Mistral-7B-Instruct-v0.3", # Using a simpler model for testing
155
- huggingfacehub_api_token=api_token,
156
- task="text-generation" )
157
 
158
- # Quick test
159
- test = llm("Say hello")
160
- print(f"Test response: {test}")
161
-
162
- return llm
163
 
164
  except Exception as e:
165
  print(f"LLM initialization error: {str(e)}")
166
- raise
167
 
168
 
169
  class ChatSession:
@@ -278,14 +272,12 @@ try:
278
  print("Database initialized successfully")
279
 
280
  print("Initializing LLM...")
281
- llm = initialize_llm()
282
- if llm is None:
283
  raise ValueError("LLM initialization failed")
284
  print("LLM initialized successfully")
285
 
286
- print("Creating LLM chain...")
287
- llm_chain = LLMChain(llm=llm, prompt=prompt)
288
- print("LLM chain created successfully")
289
 
290
  except Exception as e:
291
  print(f"Fatal initialization error: {e}")
@@ -472,24 +464,41 @@ def get_chat_list():
472
  @app.route("/api/chat", methods=["POST"])
473
  def chat():
474
  try:
475
- # Get the user's message
476
  data = request.json
477
  user_input = data.get("message", "")
478
  print(f"Received message: {user_input}")
479
 
 
 
 
 
 
 
480
  # Very simple prompt for testing
481
- prompt = f"You are a helpful assistant. Keep your response short and simple. User says: {user_input}"
 
 
482
 
483
  try:
484
- # Get response directly from model
485
- response = llm(prompt)
486
- print(f"Raw response received: {response}")
 
 
 
 
 
 
 
 
 
 
 
 
487
 
488
- # Return whatever response we get
489
- return jsonify({
490
- "success": True,
491
- "response": response
492
- })
493
 
494
  except Exception as e:
495
  print(f"Model error: {str(e)}")
 
142
  conn.commit()
143
 
144
  def initialize_llm():
145
+ """Initialize the LLM using InferenceClient."""
146
  try:
147
+ # Get API token from environment variable
148
  api_token = os.environ.get('HF_TOKEN')
149
  if not api_token:
150
  raise ValueError("No API token found")
151
 
152
+ # Initialize the InferenceClient
153
+ client = InferenceClient(api_key=api_token)
 
 
 
154
 
155
+ print("LLM initialized successfully!")
156
+ return client
 
 
 
157
 
158
  except Exception as e:
159
  print(f"LLM initialization error: {str(e)}")
160
+ return None
161
 
162
 
163
  class ChatSession:
 
272
  print("Database initialized successfully")
273
 
274
  print("Initializing LLM...")
275
+ llm_client = initialize_llm()
276
+ if llm_client is None:
277
  raise ValueError("LLM initialization failed")
278
  print("LLM initialized successfully")
279
 
280
+
 
 
281
 
282
  except Exception as e:
283
  print(f"Fatal initialization error: {e}")
 
464
  @app.route("/api/chat", methods=["POST"])
465
  def chat():
466
  try:
467
+ # Get the user's message from the request
468
  data = request.json
469
  user_input = data.get("message", "")
470
  print(f"Received message: {user_input}")
471
 
472
+ if not user_input:
473
+ return jsonify({
474
+ "success": False,
475
+ "response": "No message provided."
476
+ })
477
+
478
  # Very simple prompt for testing
479
+ messages = [
480
+ {"role": "user", "content": user_input}
481
+ ]
482
 
483
  try:
484
+ # Get response from the model using InferenceClient
485
+ if llm_client:
486
+ completion = llm_client.chat.completions.create(
487
+ model="mistralai/Mistral-7B-Instruct-v0.3", # Replace with the appropriate model
488
+ messages=messages,
489
+ max_tokens=150 # Adjust the max tokens as needed
490
+ )
491
+ response = completion['choices'][0]['message']['content']
492
+ print(f"Raw response received: {response}")
493
+
494
+ # Return the model's response
495
+ return jsonify({
496
+ "success": True,
497
+ "response": response
498
+ })
499
 
500
+ else:
501
+ raise ValueError("LLM client not initialized properly.")
 
 
 
502
 
503
  except Exception as e:
504
  print(f"Model error: {str(e)}")