Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,58 +1,43 @@
|
|
1 |
-
import faiss
|
2 |
-
import numpy as np
|
3 |
-
import torch
|
4 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
5 |
import gradio as gr
|
6 |
-
|
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 |
-
print("⚠️ FAISS match too weak, using model instead.")
|
49 |
-
inputs = tokenizer(user_query, return_tensors="pt")
|
50 |
-
outputs = model.generate(**inputs, max_new_tokens=150)
|
51 |
-
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
52 |
-
|
53 |
-
return response
|
54 |
-
|
55 |
-
|
56 |
-
# Gradio UI
|
57 |
-
iface = gr.Interface(fn=chatbot_response, inputs="text", outputs="text", title="ASA Microfinance Chatbot")
|
58 |
-
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
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):
|
7 |
+
"""Formats input in Alpaca/LLaMA style"""
|
8 |
+
prompt = f"""{system_prompt}
|
9 |
+
|
10 |
+
### Instruction:
|
11 |
+
{user_input}
|
12 |
+
|
13 |
+
### Response:
|
14 |
+
"""
|
15 |
+
return prompt
|
16 |
+
|
17 |
+
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
18 |
+
formatted_prompt = format_alpaca_prompt(message, system_message)
|
19 |
+
|
20 |
+
response = client.text_generation(
|
21 |
+
formatted_prompt,
|
22 |
+
max_new_tokens=max_tokens,
|
23 |
+
temperature=temperature,
|
24 |
+
top_p=top_p,
|
25 |
+
)
|
26 |
+
|
27 |
+
# ✅ Extract only the response
|
28 |
+
cleaned_response = response.split("### Response:")[-1].strip()
|
29 |
+
|
30 |
+
yield cleaned_response # ✅ Output only the answer
|
31 |
+
|
32 |
+
demo = gr.ChatInterface(
|
33 |
+
respond,
|
34 |
+
additional_inputs=[
|
35 |
+
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
36 |
+
gr.Slider(minimum=1, maximum=250, value=128, step=1, label="Max new tokens"),
|
37 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
38 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
39 |
+
],
|
40 |
+
)
|
41 |
+
|
42 |
+
if __name__ == "__main__":
|
43 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|