psy7743 commited on
Commit
8d8e583
·
verified ·
1 Parent(s): 52d6d7b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -50
app.py CHANGED
@@ -11,21 +11,20 @@ from llama_cpp import Llama
11
  import requests
12
  from pathlib import Path
13
 
14
- # ---------------------- Downloading model ----------------------
15
 
16
  MODEL_URL = "https://huggingface.co/datasets/psy7743/llama3-8b-instruct-Q8_0.gguf/resolve/main/llama3-8b-instruct-Q8_0.gguf"
17
  MODEL_PATH = "llama3-8b-instruct-Q8_0.gguf"
18
 
19
  if not Path(MODEL_PATH).exists():
20
- print("📥 Downloading LLaMA model from Hugging Face Datasets...")
21
  response = requests.get(MODEL_URL, stream=True)
22
  with open(MODEL_PATH, "wb") as f:
23
  for chunk in response.iter_content(chunk_size=8192):
24
- if chunk:
25
- f.write(chunk)
26
  print("✅ Download complete!")
27
 
28
- # ---------------------- Data Loading & Cleaning ----------------------
29
 
30
  df = pd.read_csv("jupiter_faqs.csv")
31
 
@@ -39,25 +38,23 @@ df['clean_question'] = df['question'].apply(clean_text)
39
  df['clean_answer'] = df['answer'].apply(clean_text)
40
  df['document'] = df.apply(lambda row: f"Question: {row['clean_question']}\nAnswer: {row['clean_answer']}", axis=1)
41
 
42
- # ---------------------- Embedding Model ----------------------
43
 
44
  embedding_model = SentenceTransformer('all-mpnet-base-v2')
45
  df['embedding'] = df['clean_question'].apply(lambda x: embedding_model.encode(x).tolist())
46
  df['uid'] = [str(uuid.uuid4()) for _ in range(len(df))]
47
 
48
- # ---------------------- Persistent ChromaDB Setup ----------------------
49
 
50
  persist_dir = "chroma_qa_db"
51
  chroma_client = chromadb.PersistentClient(path=persist_dir, settings=Settings())
52
  collection_name = "qa_collection"
53
 
54
- # Reset if exists
55
  if collection_name in [c.name for c in chroma_client.list_collections()]:
56
  chroma_client.delete_collection(name=collection_name)
57
 
58
  collection = chroma_client.get_or_create_collection(name=collection_name)
59
 
60
- # Add data to collection if empty
61
  if len(collection.get()["ids"]) == 0:
62
  collection.add(
63
  documents=df['document'].tolist(),
@@ -65,7 +62,7 @@ if len(collection.get()["ids"]) == 0:
65
  ids=df['uid'].astype(str).tolist()
66
  )
67
 
68
- # ---------------------- LLaMA Model Load ----------------------
69
 
70
  llm = Llama(
71
  model_path=MODEL_PATH,
@@ -74,7 +71,7 @@ llm = Llama(
74
  n_gpu_layers=-1,
75
  )
76
 
77
- # ---------------------- Retrieval & Inference ----------------------
78
 
79
  def search_chroma(query, n_results=5):
80
  query_embedding = embedding_model.encode(query).tolist()
@@ -85,7 +82,7 @@ def search_chroma(query, n_results=5):
85
  )
86
  return results["documents"][0]
87
 
88
- def get_inference_system(user_query):
89
  docs = search_chroma(user_query)
90
  context_str = "\n\n".join(docs)
91
 
@@ -97,13 +94,7 @@ def get_inference_system(user_query):
97
  - Do not hallucinate or make up answers.
98
  - Keep the tone friendly."""
99
 
100
- prompt = (
101
- sys_prompt
102
- + "\n\n"
103
- + "context:\n" + context_str
104
- + "\n\nQuestion: " + user_query
105
- + "\nAnswer:"
106
- )
107
 
108
  response = llm(
109
  prompt,
@@ -113,39 +104,26 @@ def get_inference_system(user_query):
113
  stop=["Q:", "\n"],
114
  echo=True
115
  )
116
-
117
  return response["choices"][0]["text"].split("Answer:")[-1].strip()
118
 
119
  # ---------------------- Gradio Interface ----------------------
120
 
121
- all_suggestions = list(df['clean_question'])
122
-
123
- def autocomplete_suggestions(prefix):
124
- prefix = prefix.lower()
125
- return [s for s in all_suggestions if s.startswith(prefix)][:5]
126
-
127
- def llama_chat(user_input, history):
128
- response = get_inference_system(user_input)
129
- history.append({"role": "user", "content": user_input})
130
- history.append({"role": "assistant", "content": response})
131
- return history, gr.update(value="")
132
-
133
- with gr.Blocks() as demo:
134
- gr.Markdown("## 🦙 LLaMA-3 FAQ Bot — Ask me anything about Jupiter Money!")
135
-
136
- chatbot = gr.Chatbot(label="LLaMA Chat", type='messages')
137
- state = gr.State([])
138
-
139
- with gr.Column():
140
- txt = gr.Textbox(show_label=False, placeholder="Type your question...")
141
- suggestion_md = gr.Markdown("") # Use Markdown instead of HighlightedText
142
-
143
- def update_suggestions(text):
144
- suggestions = autocomplete_suggestions(text)
145
- suggestion_text = "**Suggestions:**\n" + "\n".join(f"- {s}" for s in suggestions) if suggestions else ""
146
- return gr.Markdown.update(value=suggestion_text)
147
-
148
- txt.change(fn=update_suggestions, inputs=txt, outputs=suggestion_md)
149
- txt.submit(fn=llama_chat, inputs=[txt, state], outputs=[chatbot, txt])
150
 
151
- demo.launch(share=True)
 
 
11
  import requests
12
  from pathlib import Path
13
 
14
+ # ---------------------- Download Model ----------------------
15
 
16
  MODEL_URL = "https://huggingface.co/datasets/psy7743/llama3-8b-instruct-Q8_0.gguf/resolve/main/llama3-8b-instruct-Q8_0.gguf"
17
  MODEL_PATH = "llama3-8b-instruct-Q8_0.gguf"
18
 
19
  if not Path(MODEL_PATH).exists():
20
+ print("📥 Downloading LLaMA model...")
21
  response = requests.get(MODEL_URL, stream=True)
22
  with open(MODEL_PATH, "wb") as f:
23
  for chunk in response.iter_content(chunk_size=8192):
24
+ f.write(chunk)
 
25
  print("✅ Download complete!")
26
 
27
+ # ---------------------- Load Data ----------------------
28
 
29
  df = pd.read_csv("jupiter_faqs.csv")
30
 
 
38
  df['clean_answer'] = df['answer'].apply(clean_text)
39
  df['document'] = df.apply(lambda row: f"Question: {row['clean_question']}\nAnswer: {row['clean_answer']}", axis=1)
40
 
41
+ # ---------------------- Embeddings ----------------------
42
 
43
  embedding_model = SentenceTransformer('all-mpnet-base-v2')
44
  df['embedding'] = df['clean_question'].apply(lambda x: embedding_model.encode(x).tolist())
45
  df['uid'] = [str(uuid.uuid4()) for _ in range(len(df))]
46
 
47
+ # ---------------------- ChromaDB ----------------------
48
 
49
  persist_dir = "chroma_qa_db"
50
  chroma_client = chromadb.PersistentClient(path=persist_dir, settings=Settings())
51
  collection_name = "qa_collection"
52
 
 
53
  if collection_name in [c.name for c in chroma_client.list_collections()]:
54
  chroma_client.delete_collection(name=collection_name)
55
 
56
  collection = chroma_client.get_or_create_collection(name=collection_name)
57
 
 
58
  if len(collection.get()["ids"]) == 0:
59
  collection.add(
60
  documents=df['document'].tolist(),
 
62
  ids=df['uid'].astype(str).tolist()
63
  )
64
 
65
+ # ---------------------- LLaMA ----------------------
66
 
67
  llm = Llama(
68
  model_path=MODEL_PATH,
 
71
  n_gpu_layers=-1,
72
  )
73
 
74
+ # ---------------------- Inference ----------------------
75
 
76
  def search_chroma(query, n_results=5):
77
  query_embedding = embedding_model.encode(query).tolist()
 
82
  )
83
  return results["documents"][0]
84
 
85
+ def generate_response(user_query: str) -> str:
86
  docs = search_chroma(user_query)
87
  context_str = "\n\n".join(docs)
88
 
 
94
  - Do not hallucinate or make up answers.
95
  - Keep the tone friendly."""
96
 
97
+ prompt = f"{sys_prompt}\n\ncontext:\n{context_str}\n\nQuestion: {user_query}\nAnswer:"
 
 
 
 
 
 
98
 
99
  response = llm(
100
  prompt,
 
104
  stop=["Q:", "\n"],
105
  echo=True
106
  )
 
107
  return response["choices"][0]["text"].split("Answer:")[-1].strip()
108
 
109
  # ---------------------- Gradio Interface ----------------------
110
 
111
+ def gradio_chat_interface(message, history):
112
+ reply = generate_response(message)
113
+ history = history + [(message, reply)]
114
+ return history, history
115
+
116
+ demo = gr.ChatInterface(
117
+ fn=generate_response,
118
+ title="🦙 LLaMA-3 FAQ Chatbot",
119
+ chatbot=gr.Chatbot(label="Ask me anything about Jupiter Money!"),
120
+ examples=[
121
+ "What is Jupiter Edge credit card?",
122
+ "What happens if I miss a payment?",
123
+ "How to change billing address?"
124
+ ],
125
+ cache_examples=False
126
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
127
 
128
+ if __name__ == "__main__":
129
+ demo.launch(share=True)