Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,81 +1,47 @@
|
|
1 |
-
import os
|
2 |
-
import faiss
|
3 |
-
import numpy as np
|
4 |
-
import torch
|
5 |
import gradio as gr
|
6 |
-
from
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
def embed(text):
|
49 |
-
"""Convert text to FAISS-compatible vector."""
|
50 |
-
tokens = tokenizer.encode(text, add_special_tokens=True)
|
51 |
-
return np.array(tokens, dtype=np.float32).reshape(1, -1)
|
52 |
-
|
53 |
-
def chatbot_response(user_query):
|
54 |
-
"""Fetches response from FAISS or falls back to the model."""
|
55 |
-
query_vector = embed(user_query)
|
56 |
-
D, I = faiss_index.search(query_vector, k=1)
|
57 |
-
|
58 |
-
print(f"π Closest FAISS match index: {I[0][0]}, Distance: {D[0][0]}")
|
59 |
-
|
60 |
-
if D[0][0] < THRESHOLD and 0 <= I[0][0] < len(dataset):
|
61 |
-
response = dataset[I[0][0]].strip()
|
62 |
-
print("β
FAISS response used!")
|
63 |
-
else:
|
64 |
-
print("β οΈ FAISS match too weak, generating response using model.")
|
65 |
-
inputs = tokenizer(user_query, return_tensors="pt")
|
66 |
-
outputs = model.generate(**inputs, max_new_tokens=150)
|
67 |
-
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
68 |
-
|
69 |
-
return response
|
70 |
-
|
71 |
-
# πΉ Gradio UI
|
72 |
-
iface = gr.Interface(
|
73 |
-
fn=chatbot_response,
|
74 |
-
inputs="text",
|
75 |
-
outputs="text",
|
76 |
-
title="ASA Microfinance Chatbot",
|
77 |
-
description="A chatbot that provides information using FAISS and a language model."
|
78 |
)
|
79 |
|
80 |
if __name__ == "__main__":
|
81 |
-
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from huggingface_hub import InferenceClient
|
3 |
+
|
4 |
+
client = InferenceClient("Futuresony/future_ai_12_10_2024.gguf")
|
5 |
+
|
6 |
+
def format_alpaca_prompt(user_input, system_prompt, history):
|
7 |
+
"""Formats input in Alpaca/LLaMA style"""
|
8 |
+
history_str = "\n".join([f"### Instruction:\n{h[0]}\n### Response:\n{h[1]}" for h in history])
|
9 |
+
prompt = f"""{system_prompt}
|
10 |
+
{history_str}
|
11 |
+
|
12 |
+
### Instruction:
|
13 |
+
{user_input}
|
14 |
+
|
15 |
+
### Response:
|
16 |
+
"""
|
17 |
+
return prompt
|
18 |
+
|
19 |
+
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
20 |
+
formatted_prompt = format_alpaca_prompt(message, system_message, history)
|
21 |
+
|
22 |
+
response = client.text_generation(
|
23 |
+
formatted_prompt,
|
24 |
+
max_new_tokens=max_tokens,
|
25 |
+
temperature=temperature,
|
26 |
+
top_p=top_p,
|
27 |
+
)
|
28 |
+
|
29 |
+
# β
Extract only the response
|
30 |
+
cleaned_response = response.split("### Response:")[-1].strip()
|
31 |
+
|
32 |
+
history.append((message, cleaned_response)) # β
Update history with the new message and response
|
33 |
+
|
34 |
+
yield cleaned_response # β
Output only the answer
|
35 |
+
|
36 |
+
demo = gr.ChatInterface(
|
37 |
+
respond,
|
38 |
+
additional_inputs=[
|
39 |
+
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
40 |
+
gr.Slider(minimum=1, maximum=250, value=128, step=1, label="Max new tokens"),
|
41 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.9, step=0.1, label="Temperature"),
|
42 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.99, step=0.01, label="Top-p (nucleus sampling)"),
|
43 |
+
],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
)
|
45 |
|
46 |
if __name__ == "__main__":
|
47 |
+
demo.launch()
|