Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,12 +7,23 @@ import numpy as np
|
|
| 7 |
import torch
|
| 8 |
from sklearn.neighbors import NearestNeighbors
|
| 9 |
from sentence_transformers import SentenceTransformer
|
|
|
|
| 10 |
|
| 11 |
# --- CONFIGURATION ---
|
| 12 |
-
HF_TOKEN = os.getenv("HF_TOKEN", "").strip()
|
| 13 |
-
HF_MODEL = "HuggingFaceH4/zephyr-7b-beta" # Change if you want
|
| 14 |
-
HF_API_URL = f"https://api-inference.huggingface.co/models/{HF_MODEL}"
|
| 15 |
-
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
FILES = ["main1.txt", "main2.txt", "main3.txt", "main4.txt", "main5.txt", "main6.txt"] # Your text files
|
| 18 |
EMBEDDING_MODEL = "sentence-transformers/all-MiniLM-L6-v2" # Light and fast
|
|
@@ -90,33 +101,21 @@ User Question: {question}
|
|
| 90 |
Answer:"""
|
| 91 |
return prompt
|
| 92 |
|
| 93 |
-
def respond(message, history):
|
| 94 |
-
try:
|
| 95 |
-
prompt = build_prompt(message)
|
| 96 |
-
|
| 97 |
-
payload = {
|
| 98 |
-
"inputs": prompt,
|
| 99 |
-
"parameters": {"temperature": 0.2, "max_new_tokens": 400},
|
| 100 |
-
}
|
| 101 |
-
|
| 102 |
-
response = requests.post(HF_API_URL, headers=headers, json=payload, timeout=30)
|
| 103 |
-
response.raise_for_status()
|
| 104 |
-
output = response.json()
|
| 105 |
-
generated_text = output[0]["generated_text"]
|
| 106 |
-
match = re.search(r"Answer:(.*)", generated_text, re.DOTALL)
|
| 107 |
-
answer = generated_text[len(prompt):].strip()
|
| 108 |
-
|
| 109 |
|
| 110 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
except Exception as e:
|
| 112 |
-
print("
|
| 113 |
-
answer = "❌ Error
|
| 114 |
|
| 115 |
if history is None:
|
| 116 |
history = []
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
return answer
|
| 120 |
|
| 121 |
# --- INIT SECTION ---
|
| 122 |
|
|
|
|
| 7 |
import torch
|
| 8 |
from sklearn.neighbors import NearestNeighbors
|
| 9 |
from sentence_transformers import SentenceTransformer
|
| 10 |
+
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
|
| 11 |
|
| 12 |
# --- CONFIGURATION ---
|
| 13 |
+
#HF_TOKEN = os.getenv("HF_TOKEN", "").strip()
|
| 14 |
+
#HF_MODEL = "HuggingFaceH4/zephyr-7b-beta" # Change if you want
|
| 15 |
+
#HF_API_URL = f"https://api-inference.huggingface.co/models/{HF_MODEL}"
|
| 16 |
+
#headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
print("🔄 Loading local Falcon model...")
|
| 21 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 22 |
+
falcon_model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 23 |
+
pipe = pipeline("text-generation", model=falcon_model, tokenizer=tokenizer)
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
|
| 27 |
|
| 28 |
FILES = ["main1.txt", "main2.txt", "main3.txt", "main4.txt", "main5.txt", "main6.txt"] # Your text files
|
| 29 |
EMBEDDING_MODEL = "sentence-transformers/all-MiniLM-L6-v2" # Light and fast
|
|
|
|
| 101 |
Answer:"""
|
| 102 |
return prompt
|
| 103 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
|
| 106 |
+
def respond(message, history):
|
| 107 |
+
prompt = build_prompt(message)
|
| 108 |
+
try:
|
| 109 |
+
output = pipe(prompt, max_new_tokens=300, temperature=0.2)
|
| 110 |
+
answer = output[0]["generated_text"].split("Answer:")[-1].strip()
|
| 111 |
except Exception as e:
|
| 112 |
+
print("Error:", e)
|
| 113 |
+
answer = "❌ Error generating response."
|
| 114 |
|
| 115 |
if history is None:
|
| 116 |
history = []
|
| 117 |
+
history.append((message, answer))
|
| 118 |
+
return answer, history
|
|
|
|
| 119 |
|
| 120 |
# --- INIT SECTION ---
|
| 121 |
|