Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 = "
|
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 |
-
|
22 |
-
client =
|
23 |
-
|
24 |
-
|
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 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
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 |
-
|
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":
|
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)
|