Goated121 commited on
Commit
61df4df
·
verified ·
1 Parent(s): a290a45

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -63
app.py CHANGED
@@ -3,9 +3,8 @@ import faiss
3
  import pickle
4
  import numpy as np
5
  from sentence_transformers import SentenceTransformer
6
- from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
7
-
8
  import os
 
9
  print("Files in current directory:", os.listdir())
10
 
11
  # -----------------------------
@@ -17,34 +16,11 @@ index = faiss.read_index("faiss_index.bin")
17
  chunks = pickle.load(open("chunks.pkl", "rb"))
18
  metadata = pickle.load(open("metadata.pkl", "rb"))
19
 
20
- # -----------------------------
21
- # Load free HF small LLM
22
- # -----------------------------
23
- # Using distilgpt2 as it doesn't need a token
24
- model_name = "distilgpt2"
25
-
26
- tokenizer = AutoTokenizer.from_pretrained(model_name)
27
- model = AutoModelForCausalLM.from_pretrained(
28
- model_name,
29
- )
30
-
31
- generator = pipeline(
32
- "text-generation",
33
- model=model,
34
- tokenizer=tokenizer,
35
- max_new_tokens=150,
36
- do_sample=True,
37
- temperature=0.6
38
- )
39
-
40
- print("LLM loaded successfully!")
41
-
42
  # -----------------------------
43
  # Intent detection
44
  # -----------------------------
45
  def detect_query(query):
46
  query = query.lower()
47
-
48
  animal = None
49
  topic = None
50
 
@@ -78,7 +54,6 @@ def retrieve_context(query):
78
  filtered_indices = list(range(len(chunks)))
79
 
80
  query_embedding = embed_model.encode([query])
81
-
82
  filtered_embeddings = np.array([index.reconstruct(i) for i in filtered_indices])
83
  distances = np.linalg.norm(filtered_embeddings - query_embedding, axis=1)
84
  top_indices = distances.argsort()[:2]
@@ -88,47 +63,16 @@ def retrieve_context(query):
88
  real_index = filtered_indices[idx]
89
  context += chunks[real_index] + "\n"
90
 
91
- return context
92
 
93
  # -----------------------------
94
- # Chat function
95
  # -----------------------------
96
  def chat(user_input):
97
  context = retrieve_context(user_input)
98
-
99
- prompt = f"""
100
- You are a livestock expert assistant.
101
-
102
- Use ONLY the information below to answer.
103
- If answer is not present, say "I don't know".
104
-
105
- Context:
106
- {context}
107
-
108
- Question:
109
- {user_input}
110
-
111
- Answer in short, clear, and complete sentences.
112
- """
113
-
114
- response = generator(
115
- prompt,
116
- max_new_tokens=100,
117
- do_sample=True,
118
- temperature=0.6,
119
- pad_token_id=tokenizer.eos_token_id
120
- )
121
-
122
- text = response[0]["generated_text"]
123
-
124
- # Remove prompt repetition
125
- if prompt.strip() in text:
126
- text = text.split(prompt.strip())[-1].strip()
127
-
128
- # Keep only first paragraph or sentence to avoid repetition
129
- text = text.split("\n")[0].strip()
130
-
131
- return text
132
 
133
  # -----------------------------
134
  # Gradio UI
@@ -137,5 +81,6 @@ gr.Interface(
137
  fn=chat,
138
  inputs="text",
139
  outputs="text",
140
- title="Livestock Chatbot"
 
141
  ).launch()
 
3
  import pickle
4
  import numpy as np
5
  from sentence_transformers import SentenceTransformer
 
 
6
  import os
7
+
8
  print("Files in current directory:", os.listdir())
9
 
10
  # -----------------------------
 
16
  chunks = pickle.load(open("chunks.pkl", "rb"))
17
  metadata = pickle.load(open("metadata.pkl", "rb"))
18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  # -----------------------------
20
  # Intent detection
21
  # -----------------------------
22
  def detect_query(query):
23
  query = query.lower()
 
24
  animal = None
25
  topic = None
26
 
 
54
  filtered_indices = list(range(len(chunks)))
55
 
56
  query_embedding = embed_model.encode([query])
 
57
  filtered_embeddings = np.array([index.reconstruct(i) for i in filtered_indices])
58
  distances = np.linalg.norm(filtered_embeddings - query_embedding, axis=1)
59
  top_indices = distances.argsort()[:2]
 
63
  real_index = filtered_indices[idx]
64
  context += chunks[real_index] + "\n"
65
 
66
+ return context.strip()
67
 
68
  # -----------------------------
69
+ # Chat function (RAG only)
70
  # -----------------------------
71
  def chat(user_input):
72
  context = retrieve_context(user_input)
73
+ if not context:
74
+ return "I don't know."
75
+ return context
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
 
77
  # -----------------------------
78
  # Gradio UI
 
81
  fn=chat,
82
  inputs="text",
83
  outputs="text",
84
+ title="Livestock Chatbot (RAG only)",
85
+ description="This chatbot answers livestock questions using only the retrieved data. No AI model is used."
86
  ).launch()