Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -11,41 +11,71 @@ from huggingface_hub import InferenceClient, hf_hub_download
|
|
11 |
HF_REPO = "Futuresony/future_ai_12_10_2024.gguf"
|
12 |
HF_TOKEN = os.getenv('HUGGINGFACEHUB_API_TOKEN') # Ensure this is set in your environment
|
13 |
|
14 |
-
|
|
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
prompt = f"""{system_prompt}
|
19 |
|
20 |
-
|
21 |
-
|
|
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
|
|
|
|
|
|
27 |
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
response = client.text_generation(
|
31 |
-
|
32 |
max_new_tokens=max_tokens,
|
33 |
temperature=temperature,
|
34 |
top_p=top_p,
|
35 |
)
|
36 |
|
37 |
-
# β
Extract only the response
|
38 |
cleaned_response = response.split("### Response:")[-1].strip()
|
39 |
-
|
40 |
-
yield cleaned_response # β
Output only the answer
|
41 |
|
|
|
|
|
|
|
|
|
|
|
42 |
demo = gr.ChatInterface(
|
43 |
respond,
|
44 |
additional_inputs=[
|
45 |
-
gr.Textbox(value="You are a
|
46 |
gr.Slider(minimum=1, maximum=250, value=128, step=1, label="Max new tokens"),
|
47 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.
|
48 |
-
gr.Slider(minimum=0.1, maximum=1.0, value=0.
|
49 |
],
|
50 |
)
|
51 |
|
|
|
11 |
HF_REPO = "Futuresony/future_ai_12_10_2024.gguf"
|
12 |
HF_TOKEN = os.getenv('HUGGINGFACEHUB_API_TOKEN') # Ensure this is set in your environment
|
13 |
|
14 |
+
# πΉ FAISS Index Path
|
15 |
+
FAISS_PATH = "asa_faiss.index"
|
16 |
|
17 |
+
# πΉ Load Sentence Transformer for Embeddings
|
18 |
+
embedder = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
|
|
|
19 |
|
20 |
+
# πΉ Load FAISS Index from Hugging Face
|
21 |
+
faiss_local_path = hf_hub_download(HF_REPO, "asa_faiss.index", token=HF_TOKEN)
|
22 |
+
faiss_index = faiss.read_index(faiss_local_path)
|
23 |
|
24 |
+
# πΉ Initialize Hugging Face Model Client
|
25 |
+
client = InferenceClient(model=HF_REPO, token=HF_TOKEN)
|
26 |
+
|
27 |
+
# πΉ Retrieve Relevant FAISS Data
|
28 |
+
def retrieve_faiss_knowledge(user_query, top_k=3):
|
29 |
+
query_embedding = embedder.encode([user_query], convert_to_tensor=True).cpu().numpy()
|
30 |
+
distances, indices = faiss_index.search(query_embedding, top_k)
|
31 |
+
|
32 |
+
retrieved_texts = []
|
33 |
+
for idx in indices[0]: # Extract top_k results
|
34 |
+
if idx != -1: # Ensure valid index
|
35 |
+
retrieved_texts.append(f"Example {idx}: (Extracted FAISS Data)")
|
36 |
|
37 |
+
return "\n".join(retrieved_texts) if retrieved_texts else "**No relevant FAISS data found.**"
|
38 |
+
|
39 |
+
# πΉ Chatbot Response Function (Forcing FAISS Context)
|
40 |
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
41 |
+
faiss_context = retrieve_faiss_knowledge(message)
|
42 |
+
|
43 |
+
# π₯ Force the model to use FAISS
|
44 |
+
full_prompt = f"""### System Instruction:
|
45 |
+
You MUST use the provided FAISS data to generate your response.
|
46 |
+
If no FAISS data is found, return "I don't have enough information."
|
47 |
+
|
48 |
+
### Retrieved FAISS Data:
|
49 |
+
{faiss_context}
|
50 |
+
|
51 |
+
### User Query:
|
52 |
+
{message}
|
53 |
+
|
54 |
+
### Response:
|
55 |
+
"""
|
56 |
|
57 |
response = client.text_generation(
|
58 |
+
full_prompt,
|
59 |
max_new_tokens=max_tokens,
|
60 |
temperature=temperature,
|
61 |
top_p=top_p,
|
62 |
)
|
63 |
|
64 |
+
# β
Extract only the model-generated response
|
65 |
cleaned_response = response.split("### Response:")[-1].strip()
|
|
|
|
|
66 |
|
67 |
+
history.append((message, cleaned_response)) # β
Update chat history
|
68 |
+
|
69 |
+
yield cleaned_response # β
Output the response
|
70 |
+
|
71 |
+
# πΉ Gradio Chat Interface
|
72 |
demo = gr.ChatInterface(
|
73 |
respond,
|
74 |
additional_inputs=[
|
75 |
+
gr.Textbox(value="You are a knowledge assistant that must use FAISS context.", label="System message"),
|
76 |
gr.Slider(minimum=1, maximum=250, value=128, step=1, label="Max new tokens"),
|
77 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.9, step=0.1, label="Temperature"),
|
78 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.99, step=0.01, label="Top-p (nucleus sampling)"),
|
79 |
],
|
80 |
)
|
81 |
|