Pamudu13 commited on
Commit
04b809a
·
verified ·
1 Parent(s): a7cd9ba

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -19
app.py CHANGED
@@ -1,3 +1,5 @@
 
 
1
  from flask import Flask, request, jsonify, render_template
2
  import fitz # PyMuPDF for PDF text extraction
3
  import faiss # FAISS for vector search
@@ -11,18 +13,19 @@ app = Flask(__name__, template_folder=os.getcwd())
11
 
12
  # Default settings
13
  class ChatConfig:
14
- MODEL = "meta-llama/Llama-3.1-8B-Instruct" # Change back to Gemma
15
  DEFAULT_SYSTEM_MSG = "You are an AI assistant answering only based on the uploaded PDF."
16
  DEFAULT_MAX_TOKENS = 512
17
  DEFAULT_TEMP = 0.3
18
  DEFAULT_TOP_P = 0.95
19
 
20
  # Get the token from environment variable
21
- HF_TOKEN = os.getenv('HUGGINGFACE_TOKEN')
22
- client = InferenceClient(
23
- ChatConfig.MODEL,
24
- token=HF_TOKEN
25
  )
 
26
  embed_model = SentenceTransformer("all-MiniLM-L6-v2", cache_folder="/tmp")
27
  vector_dim = 384 # Embedding size
28
  index = faiss.IndexFlatL2(vector_dim) # FAISS index
@@ -92,21 +95,16 @@ def generate_response(
92
  messages.append({"role": "assistant", "content": bot_msg})
93
 
94
  try:
95
- response = ""
96
- for chunk in client.chat_completion(
97
- messages,
98
- max_tokens=max_tokens,
99
- stream=True,
100
- temperature=temperature,
101
- top_p=top_p,
102
- ):
103
- token = chunk.choices[0].delta.content or ""
104
- response += token
105
- yield response
106
  except Exception as e:
107
  print(f"Error generating response: {str(e)}")
108
- yield "I apologize, but I encountered an error while generating the response. Please try again."
109
-
110
  @app.route('/')
111
  def index():
112
  """Serve the HTML page for the user interface"""
@@ -148,7 +146,7 @@ def ask_question():
148
  message = request.json.get('message')
149
  history = request.json.get('history', [])
150
  response = generate_response(message, history)
151
- return jsonify({"response": "".join(response)}) # Join all streamed responses
152
 
153
  if __name__ == '__main__':
154
  app.run(debug=True)
 
1
+ from openai import OpenAI
2
+ from os import getenv
3
  from flask import Flask, request, jsonify, render_template
4
  import fitz # PyMuPDF for PDF text extraction
5
  import faiss # FAISS for vector search
 
13
 
14
  # Default settings
15
  class ChatConfig:
16
+ MODEL = "google/gemma-7b-it:free" # Use OpenRouter's Gemma model
17
  DEFAULT_SYSTEM_MSG = "You are an AI assistant answering only based on the uploaded PDF."
18
  DEFAULT_MAX_TOKENS = 512
19
  DEFAULT_TEMP = 0.3
20
  DEFAULT_TOP_P = 0.95
21
 
22
  # Get the token from environment variable
23
+ OPENROUTER_API_KEY = getenv('OPENROUTER_API_KEY')
24
+ client = OpenAI(
25
+ base_url="https://openrouter.ai/api/v1",
26
+ api_key=OPENROUTER_API_KEY,
27
  )
28
+
29
  embed_model = SentenceTransformer("all-MiniLM-L6-v2", cache_folder="/tmp")
30
  vector_dim = 384 # Embedding size
31
  index = faiss.IndexFlatL2(vector_dim) # FAISS index
 
95
  messages.append({"role": "assistant", "content": bot_msg})
96
 
97
  try:
98
+ # Use OpenRouter to get the response
99
+ completion = client.chat.completions.create(
100
+ model="google/gemma-7b-it:free",
101
+ messages=messages
102
+ )
103
+ return completion.choices[0].message.content
 
 
 
 
 
104
  except Exception as e:
105
  print(f"Error generating response: {str(e)}")
106
+ return "I apologize, but I encountered an error while generating the response. Please try again."
107
+
108
  @app.route('/')
109
  def index():
110
  """Serve the HTML page for the user interface"""
 
146
  message = request.json.get('message')
147
  history = request.json.get('history', [])
148
  response = generate_response(message, history)
149
+ return jsonify({"response": response})
150
 
151
  if __name__ == '__main__':
152
  app.run(debug=True)