Spaces:
Sleeping
Sleeping
new
Browse files
app.py
CHANGED
@@ -1,77 +1,84 @@
|
|
1 |
-
# import gradio as gr
|
2 |
-
|
3 |
-
# gr.load("models/HuggingFaceH4/zephyr-7b-alpha").launch()
|
4 |
-
|
5 |
-
import os
|
6 |
-
import numpy as np
|
7 |
import gradio as gr
|
8 |
-
from
|
9 |
-
import
|
10 |
-
|
11 |
-
|
12 |
-
def load_embeddings(embeddings_folder='embeddings'):
|
13 |
-
all_embeddings = []
|
14 |
-
metadata = []
|
15 |
-
for file in os.listdir(embeddings_folder):
|
16 |
-
if file.endswith('.npy'):
|
17 |
-
embedding_path = os.path.join(embeddings_folder, file)
|
18 |
-
embedding = np.load(embedding_path) # Shape: (27, 384)
|
19 |
-
all_embeddings.append(embedding)
|
20 |
-
# Metadata corresponds to each .npy file
|
21 |
-
meta_info = file.replace('.npy', '') # Example: 'course_1'
|
22 |
-
metadata.extend([meta_info] * embedding.shape[0]) # Repeat metadata for each sub-embedding
|
23 |
-
|
24 |
-
# Flatten list of embeddings to shape (n * 27, 384)
|
25 |
-
all_embeddings = np.vstack(all_embeddings)
|
26 |
-
return all_embeddings, metadata
|
27 |
-
|
28 |
-
embeddings, metadata = load_embeddings()
|
29 |
-
|
30 |
-
# Step 2: Set Up FAISS Index with Flattened Embeddings
|
31 |
-
dimension = embeddings.shape[1] # Should be 384 after flattening
|
32 |
-
index = faiss.IndexFlatL2(dimension)
|
33 |
-
index.add(embeddings)
|
34 |
-
|
35 |
|
36 |
-
#
|
37 |
-
|
38 |
-
# tokenizer = AutoTokenizer.from_pretrained(model_name)
|
39 |
-
# model = AutoModelForCausalLM.from_pretrained(model_name)
|
40 |
|
41 |
-
|
42 |
-
|
43 |
-
model =
|
|
|
44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
-
#
|
47 |
-
def
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
-
#
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
return answer
|
62 |
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
)
|
75 |
|
76 |
-
|
77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from huggingface_hub import InferenceClient
|
3 |
+
import pandas as pd
|
4 |
+
import torch
|
5 |
+
from sentence_transformers import SentenceTransformer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
+
# Initialize the Hugging Face API client
|
8 |
+
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
|
|
|
|
9 |
|
10 |
+
# Load and preprocess course data for search functionality
|
11 |
+
courses_df = pd.read_csv("courses_data.csv") # Assuming courses_data.csv is already scraped
|
12 |
+
model = SentenceTransformer('distilbert-base-nli-stsb-mean-tokens')
|
13 |
+
courses_df['embedding'] = courses_df['description'].apply(lambda x: model.encode(x, convert_to_tensor=True))
|
14 |
|
15 |
+
# Define a helper function to search for courses based on user query
|
16 |
+
def search_courses(query, top_k=5):
|
17 |
+
query_embedding = model.encode(query, convert_to_tensor=True)
|
18 |
+
cosine_scores = torch.nn.functional.cosine_similarity(query_embedding, torch.stack(courses_df['embedding'].tolist()))
|
19 |
+
top_results = torch.topk(cosine_scores, k=top_k)
|
20 |
+
|
21 |
+
results = []
|
22 |
+
for idx in top_results.indices:
|
23 |
+
course = courses_df.iloc[idx.item()]
|
24 |
+
results.append(f"**{course['title']}**\n{course['description']}\nNumber of Lessons: {course['lessons']}")
|
25 |
+
return "\n\n".join(results)
|
26 |
|
27 |
+
# Modify respond function to include course search when "search: [query]" is detected
|
28 |
+
def respond(
|
29 |
+
message,
|
30 |
+
history: list[tuple[str, str]],
|
31 |
+
system_message,
|
32 |
+
max_tokens,
|
33 |
+
temperature,
|
34 |
+
top_p,
|
35 |
+
):
|
36 |
+
# Check if the message is a course search command
|
37 |
+
if message.lower().startswith("search:"):
|
38 |
+
query = message[len("search:"):].strip() # Extract search query
|
39 |
+
response = search_courses(query) # Perform course search
|
40 |
+
yield response
|
41 |
+
return
|
42 |
|
43 |
+
# Standard chat processing using Hugging Face Inference API for general chat
|
44 |
+
messages = [{"role": "system", "content": system_message}]
|
45 |
+
for val in history:
|
46 |
+
if val[0]:
|
47 |
+
messages.append({"role": "user", "content": val[0]})
|
48 |
+
if val[1]:
|
49 |
+
messages.append({"role": "assistant", "content": val[1]})
|
50 |
+
messages.append({"role": "user", "content": message})
|
|
|
51 |
|
52 |
+
response = ""
|
53 |
+
for message in client.chat_completion(
|
54 |
+
messages,
|
55 |
+
max_tokens=max_tokens,
|
56 |
+
stream=True,
|
57 |
+
temperature=temperature,
|
58 |
+
top_p=top_p,
|
59 |
+
):
|
60 |
+
token = message.choices[0].delta.content
|
61 |
+
response += token
|
62 |
+
yield response
|
63 |
|
64 |
+
# Gradio chat interface setup
|
65 |
+
demo = gr.ChatInterface(
|
66 |
+
respond,
|
67 |
+
additional_inputs=[
|
68 |
+
gr.Textbox(value="You are a helpful assistant that provides detailed information about courses on Analytics Vidhya. Please assist the user by answering questions about available courses, course content, instructors, ratings, pricing, and any other relevant details. If a user asks about course recommendations, suggest relevant courses based on their needs or preferences.", label="System message"),
|
69 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
70 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
71 |
+
gr.Slider(
|
72 |
+
minimum=0.1,
|
73 |
+
maximum=1.0,
|
74 |
+
value=0.95,
|
75 |
+
step=0.05,
|
76 |
+
label="Top-p (nucleus sampling)",
|
77 |
+
),
|
78 |
+
],
|
79 |
+
description="Type 'search: [your query]' to search for courses on Analytics Vidhya, or chat with the assistant."
|
80 |
)
|
81 |
|
82 |
+
# Launch the app
|
83 |
+
if __name__ == "__main__":
|
84 |
+
demo.launch()
|