Rohitface commited on
Commit
030fde7
·
verified ·
1 Parent(s): e96d9cf

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +85 -0
app.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import chromadb
3
+ from sentence_transformers import SentenceTransformer
4
+ import pandas as pd
5
+
6
+ # --- 1. Load Model (No changes here) ---
7
+ print("Loading sentence-transformer model...")
8
+ model = SentenceTransformer('all-MiniLM-L6-v2')
9
+ print("Model loaded.")
10
+
11
+ # --- 2. Setup ChromaDB (No changes here) ---
12
+ client = chromadb.Client()
13
+
14
+ try:
15
+ collection = client.create_collection("my_documents")
16
+ print("ChromaDB collection created.")
17
+
18
+ documents = [
19
+ "The Eiffel Tower is a wrought-iron lattice tower on the Champ de Mars in Paris, France.",
20
+ "The Great Wall of China is a series of fortifications made of stone, brick, and other materials.",
21
+ "ChromaDB is an open-source embedding database designed to store and retrieve vector embeddings.",
22
+ "Hugging Face Spaces is a platform for building, deploying, and sharing ML apps.",
23
+ "A chatbot is a software application used to conduct an on-line chat conversation."
24
+ ]
25
+
26
+ embeddings = model.encode(documents)
27
+
28
+ collection.add(
29
+ embeddings=embeddings.tolist(),
30
+ documents=documents,
31
+ ids=[f"id_{i}" for i in range(len(documents))]
32
+ )
33
+ print("Documents added to ChromaDB.")
34
+
35
+ except ValueError:
36
+ collection = client.get_collection("my_documents")
37
+ print("ChromaDB collection loaded.")
38
+
39
+
40
+ # --- 3. Define the Chatbot Logic (Minor change to fit ChatInterface) ---
41
+ def chatbot_response(message, history):
42
+ """
43
+ This function processes the user message and returns a response.
44
+ 'message' is the user's input, 'history' is the conversation history.
45
+ """
46
+ # 1. Create an embedding for the user's message
47
+ query_embedding = model.encode([message]).tolist()
48
+
49
+ # 2. Query ChromaDB to find the most relevant documents
50
+ results = collection.query(
51
+ query_embeddings=query_embedding,
52
+ n_results=2
53
+ )
54
+
55
+ retrieved_documents = results['documents'][0]
56
+
57
+ if not retrieved_documents:
58
+ return "I'm sorry, I couldn't find any information on that topic. 🤔"
59
+
60
+ # 4. Formulate the response
61
+ context = "\n- ".join(retrieved_documents)
62
+ response = f"Here's what I found related to your question:\n- {context}"
63
+
64
+ return response
65
+
66
+ # --- 4. Create the NEW Gradio Chat Interface ---
67
+ iface = gr.ChatInterface(
68
+ fn=chatbot_response,
69
+ title="ChromaDB Knowledge Bot 🤖",
70
+ description="Ask me anything! I'll search my knowledge base to find the best answer for you.",
71
+ theme="soft", # Try "soft", "glass", "monochrome", or "base"
72
+ examples=[
73
+ "What is ChromaDB?",
74
+ "Tell me about Hugging Face Spaces",
75
+ "What is the Eiffel Tower made of?"
76
+ ],
77
+ chatbot=gr.Chatbot(avatar_images=("user.png", "bot.png")), # Optional: Add avatar images
78
+ cache_examples=False,
79
+ retry_btn=None,
80
+ undo_btn=None,
81
+ clear_btn="Clear Conversation",
82
+ )
83
+
84
+ # Launch the app
85
+ iface.launch()