Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ from flask import Flask, render_template, request, jsonify
|
|
| 2 |
import subprocess
|
| 3 |
import tempfile
|
| 4 |
import os
|
|
|
|
| 5 |
from langchain_community.llms import HuggingFacePipeline
|
| 6 |
from langchain.prompts import PromptTemplate
|
| 7 |
from langchain.chains import LLMChain
|
|
@@ -15,167 +16,118 @@ import re
|
|
| 15 |
from werkzeug.utils import secure_filename
|
| 16 |
import torch
|
| 17 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 18 |
-
from huggingface_hub import login
|
| 19 |
|
| 20 |
app = Flask(__name__)
|
| 21 |
|
| 22 |
# Configuration for Hugging Face Spaces
|
| 23 |
PORT = int(os.environ.get("PORT", 7860))
|
| 24 |
|
| 25 |
-
#
|
| 26 |
-
|
| 27 |
-
os.
|
| 28 |
-
os.environ['
|
| 29 |
-
os.environ['
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
'/tmp/transformers_cache',
|
| 34 |
-
'/tmp/hf_home',
|
| 35 |
-
'/tmp/cache',
|
| 36 |
-
'/tmp/datasets_cache',
|
| 37 |
-
'/tmp/uploads'
|
| 38 |
-
]:
|
| 39 |
-
os.makedirs(directory, exist_ok=True)
|
| 40 |
-
|
| 41 |
-
# Configure upload folder inside the space
|
| 42 |
UPLOAD_FOLDER = '/tmp/uploads'
|
| 43 |
ALLOWED_EXTENSIONS = {'py'}
|
| 44 |
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
|
|
|
| 45 |
|
| 46 |
# Database configuration
|
| 47 |
DATABASE_PATH = '/tmp/chat_database.db'
|
| 48 |
|
| 49 |
-
def get_model_name():
|
| 50 |
-
"""Determine which model to use based on token availability"""
|
| 51 |
-
try:
|
| 52 |
-
hf_token = os.environ.get("HF_TOKEN")
|
| 53 |
-
if hf_token:
|
| 54 |
-
# Set token in environment and return gated model name
|
| 55 |
-
os.environ['HUGGING_FACE_HUB_TOKEN'] = hf_token
|
| 56 |
-
return "mistralai/Mistral-7B-Instruct-v0.1"
|
| 57 |
-
else:
|
| 58 |
-
# Return free model if no token
|
| 59 |
-
return "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
|
| 60 |
-
except Exception as e:
|
| 61 |
-
print(f"Error accessing token: {e}")
|
| 62 |
-
return "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
|
| 63 |
-
|
| 64 |
def initialize_model():
|
| 65 |
"""Initialize the model with appropriate settings"""
|
| 66 |
try:
|
| 67 |
-
#
|
| 68 |
-
model_name = "
|
| 69 |
print(f"Initializing model: {model_name}")
|
| 70 |
|
| 71 |
-
# Initialize tokenizer
|
| 72 |
tokenizer = AutoTokenizer.from_pretrained(
|
| 73 |
model_name,
|
| 74 |
-
cache_dir=
|
| 75 |
-
|
| 76 |
)
|
| 77 |
|
| 78 |
-
# Initialize model with
|
| 79 |
model = AutoModelForCausalLM.from_pretrained(
|
| 80 |
model_name,
|
| 81 |
-
cache_dir=
|
| 82 |
-
|
| 83 |
torch_dtype=torch.float16,
|
| 84 |
-
|
| 85 |
-
load_in_8bit=True
|
| 86 |
)
|
| 87 |
|
| 88 |
-
# Create pipeline
|
| 89 |
pipe = pipeline(
|
| 90 |
"text-generation",
|
| 91 |
model=model,
|
| 92 |
tokenizer=tokenizer,
|
| 93 |
-
max_new_tokens=
|
| 94 |
-
do_sample=True,
|
| 95 |
temperature=0.7,
|
| 96 |
top_p=0.95,
|
| 97 |
repetition_penalty=1.15,
|
| 98 |
-
|
| 99 |
)
|
| 100 |
|
| 101 |
return HuggingFacePipeline(pipeline=pipe)
|
| 102 |
except Exception as e:
|
| 103 |
print(f"Error initializing model: {e}")
|
| 104 |
-
|
| 105 |
-
try:
|
| 106 |
-
model_name = "facebook/opt-125m"
|
| 107 |
-
print(f"Trying fallback model: {model_name}")
|
| 108 |
-
|
| 109 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 110 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 111 |
-
model_name,
|
| 112 |
-
torch_dtype=torch.float16,
|
| 113 |
-
device_map="auto"
|
| 114 |
-
)
|
| 115 |
-
|
| 116 |
-
pipe = pipeline(
|
| 117 |
-
"text-generation",
|
| 118 |
-
model=model,
|
| 119 |
-
tokenizer=tokenizer,
|
| 120 |
-
max_new_tokens=512,
|
| 121 |
-
temperature=0.7,
|
| 122 |
-
top_p=0.95
|
| 123 |
-
)
|
| 124 |
-
|
| 125 |
-
return HuggingFacePipeline(pipeline=pipe)
|
| 126 |
-
except Exception as fallback_error:
|
| 127 |
-
print(f"Fallback model also failed: {fallback_error}")
|
| 128 |
-
raise
|
| 129 |
-
|
| 130 |
-
print("Starting model initialization...")
|
| 131 |
-
llm = initialize_model()
|
| 132 |
-
print("Model initialization complete!")
|
| 133 |
-
|
| 134 |
-
@contextmanager
|
| 135 |
-
def get_db_connection():
|
| 136 |
-
conn = sqlite3.connect(DATABASE_PATH)
|
| 137 |
-
conn.row_factory = sqlite3.Row
|
| 138 |
-
try:
|
| 139 |
-
yield conn
|
| 140 |
-
finally:
|
| 141 |
-
conn.close()
|
| 142 |
-
|
| 143 |
|
|
|
|
| 144 |
def init_db():
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
|
| 177 |
-
# Initialize
|
| 178 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
|
| 180 |
|
| 181 |
class ChatSession:
|
|
|
|
| 2 |
import subprocess
|
| 3 |
import tempfile
|
| 4 |
import os
|
| 5 |
+
import shutil
|
| 6 |
from langchain_community.llms import HuggingFacePipeline
|
| 7 |
from langchain.prompts import PromptTemplate
|
| 8 |
from langchain.chains import LLMChain
|
|
|
|
| 16 |
from werkzeug.utils import secure_filename
|
| 17 |
import torch
|
| 18 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
|
|
|
| 19 |
|
| 20 |
app = Flask(__name__)
|
| 21 |
|
| 22 |
# Configuration for Hugging Face Spaces
|
| 23 |
PORT = int(os.environ.get("PORT", 7860))
|
| 24 |
|
| 25 |
+
# Create and set up a writable directory in /tmp
|
| 26 |
+
CACHE_DIR = "/tmp/huggingface_cache"
|
| 27 |
+
os.makedirs(CACHE_DIR, exist_ok=True)
|
| 28 |
+
os.environ['TRANSFORMERS_CACHE'] = CACHE_DIR
|
| 29 |
+
os.environ['HF_HOME'] = CACHE_DIR
|
| 30 |
+
os.environ['XDG_CACHE_HOME'] = CACHE_DIR
|
| 31 |
+
|
| 32 |
+
# Configure upload folder
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
UPLOAD_FOLDER = '/tmp/uploads'
|
| 34 |
ALLOWED_EXTENSIONS = {'py'}
|
| 35 |
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
| 36 |
+
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
| 37 |
|
| 38 |
# Database configuration
|
| 39 |
DATABASE_PATH = '/tmp/chat_database.db'
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
def initialize_model():
|
| 42 |
"""Initialize the model with appropriate settings"""
|
| 43 |
try:
|
| 44 |
+
# Use a smaller model that's more likely to work in the Space
|
| 45 |
+
model_name = "facebook/opt-350m"
|
| 46 |
print(f"Initializing model: {model_name}")
|
| 47 |
|
| 48 |
+
# Initialize tokenizer
|
| 49 |
tokenizer = AutoTokenizer.from_pretrained(
|
| 50 |
model_name,
|
| 51 |
+
cache_dir=CACHE_DIR,
|
| 52 |
+
local_files_only=False
|
| 53 |
)
|
| 54 |
|
| 55 |
+
# Initialize model with minimal settings
|
| 56 |
model = AutoModelForCausalLM.from_pretrained(
|
| 57 |
model_name,
|
| 58 |
+
cache_dir=CACHE_DIR,
|
| 59 |
+
local_files_only=False,
|
| 60 |
torch_dtype=torch.float16,
|
| 61 |
+
low_cpu_mem_usage=True
|
|
|
|
| 62 |
)
|
| 63 |
|
| 64 |
+
# Create pipeline
|
| 65 |
pipe = pipeline(
|
| 66 |
"text-generation",
|
| 67 |
model=model,
|
| 68 |
tokenizer=tokenizer,
|
| 69 |
+
max_new_tokens=256,
|
|
|
|
| 70 |
temperature=0.7,
|
| 71 |
top_p=0.95,
|
| 72 |
repetition_penalty=1.15,
|
| 73 |
+
device_map="auto"
|
| 74 |
)
|
| 75 |
|
| 76 |
return HuggingFacePipeline(pipeline=pipe)
|
| 77 |
except Exception as e:
|
| 78 |
print(f"Error initializing model: {e}")
|
| 79 |
+
raise
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
+
# Initialize database
|
| 82 |
def init_db():
|
| 83 |
+
"""Initialize the database"""
|
| 84 |
+
try:
|
| 85 |
+
with get_db_connection() as conn:
|
| 86 |
+
conn.execute('''
|
| 87 |
+
CREATE TABLE IF NOT EXISTS chats (
|
| 88 |
+
id TEXT PRIMARY KEY,
|
| 89 |
+
title TEXT,
|
| 90 |
+
date TEXT,
|
| 91 |
+
last_message TEXT
|
| 92 |
+
)
|
| 93 |
+
''')
|
| 94 |
+
|
| 95 |
+
conn.execute('''
|
| 96 |
+
CREATE TABLE IF NOT EXISTS messages (
|
| 97 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 98 |
+
chat_id TEXT,
|
| 99 |
+
role TEXT,
|
| 100 |
+
content TEXT,
|
| 101 |
+
timestamp TEXT,
|
| 102 |
+
FOREIGN KEY (chat_id) REFERENCES chats (id)
|
| 103 |
+
)
|
| 104 |
+
''')
|
| 105 |
+
|
| 106 |
+
conn.execute('''
|
| 107 |
+
CREATE TABLE IF NOT EXISTS important_info (
|
| 108 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 109 |
+
chat_id TEXT,
|
| 110 |
+
content TEXT,
|
| 111 |
+
FOREIGN KEY (chat_id) REFERENCES chats (id)
|
| 112 |
+
)
|
| 113 |
+
''')
|
| 114 |
+
conn.commit()
|
| 115 |
+
except Exception as e:
|
| 116 |
+
print(f"Error initializing database: {e}")
|
| 117 |
+
raise
|
| 118 |
|
| 119 |
+
# Initialize the application
|
| 120 |
+
try:
|
| 121 |
+
print("Initializing database...")
|
| 122 |
+
init_db()
|
| 123 |
+
print("Database initialized successfully")
|
| 124 |
+
|
| 125 |
+
print("Starting model initialization...")
|
| 126 |
+
llm = initialize_model()
|
| 127 |
+
print("Model initialized successfully")
|
| 128 |
+
except Exception as e:
|
| 129 |
+
print(f"Initialization error: {e}")
|
| 130 |
+
raise
|
| 131 |
|
| 132 |
|
| 133 |
class ChatSession:
|