File size: 4,436 Bytes
e5224e4
a474d08
 
e5224e4
 
63ca731
a89b188
f1fc1a8
a474d08
f1fc1a8
 
 
e5224e4
 
a8eb38b
e5224e4
 
a1f2cae
e5224e4
 
 
 
 
 
 
 
 
 
 
a1f2cae
 
 
 
e5224e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4fea8d8
e0dd3be
a89b188
4fea8d8
5a2c7b1
 
 
 
 
 
 
 
a1f2cae
 
5a2c7b1
 
e5224e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dcf49e5
 
 
 
a1f2cae
dcf49e5
 
 
 
 
 
 
 
 
 
 
e5224e4
3879143
1
2
3
4
5
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
from sentence_transformers import SentenceTransformer
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.messages import AIMessage, HumanMessage
from langchain_chroma import Chroma
import gradio as gr
from huggingface_hub import InferenceClient
import os
import shutil

shutil.rmtree("/root/.cache", ignore_errors=True)
shutil.rmtree("/tmp", ignore_errors=True)

# Load environment variables
CHROMA_PATH = "chroma"
KEY = os.getenv("token")

# Hugging Face API setup
repo_id = "Qwen/Qwen2.5-7B-Instruct"

PROMPT_TEMPLATE = """
Answer the question based on the context provided. If no relevant information is found, state so.

Context:
{context}

Question:
{question}

Answer:
// Do not include the statement "Based on the provided context" in your answer. Start directly with the answer.
// If the user mentions that he does not want to know about these courses, ask him what topic he wants to learn about in the answer.

If you encounter a text message which is not related to the context, the bot must respond with "I don't have relevant information to answer that. Kindly ask queries related to free data science and machine learning courses on Analytics Vidhya."
"""

# Initialize the local embedding model
embedding_model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')


class LocalEmbeddingFunction:
    def embed_documents(self, texts):
        # Generate embeddings for a list of texts
        embeddings = embedding_model.encode(texts)
        return embeddings.tolist() if hasattr(embeddings, 'tolist') else embeddings

    def embed_query(self, query):
        # Generate an embedding for a single query string
        query_embedding = embedding_model.encode(query)
        return query_embedding.tolist() if hasattr(query_embedding, 'tolist') else query_embedding


class LLM:

    def generate_response(self, prompt):
        client = InferenceClient(
            provider="together",
            api_key = KEY
        )
        completion = client.chat.completions.create(
            model=repo_id,
            messages=[
                {
                    "role": "user",
                    "content": prompt
                }
            ],
            max_tokens=500,
            temperature=0.8
        )
        return str(completion.choices[0].message.content)


def get_embedding_function():
    return LocalEmbeddingFunction()

def get_chat_response(query, history):
    # Retrieve embeddings from the Chroma DB
    embedding_function = get_embedding_function()
    db = Chroma(persist_directory=CHROMA_PATH, embedding_function=embedding_function)

    # Search the DB
    results = db.similarity_search_with_score(query, k=5)
    context_text = "\n\n---\n\n".join([doc.page_content for doc, _score in results])

    # Create prompt with context and query
    prompt_template = ChatPromptTemplate.from_template(PROMPT_TEMPLATE)
    prompt = prompt_template.format(context=context_text, question=query)

    # Generate response using the LLM class and Hugging Face model
    model = LLM()
    response_text = model.generate_response(prompt)

    # Update the history
    history.append(AIMessage(content = response_text))

    return response_text


# Gradio Interface
def predict(message, history):
    # Initialize history if not provided
    history_langchain_format = []

    for msg in history:

        if msg['role'] == "user":
            history_langchain_format.append(HumanMessage(content=msg['content']))

        elif msg['role'] == "assistant":
            history_langchain_format.append(AIMessage(content=msg['content']))

    history_langchain_format.append(HumanMessage(content=message))

    # Get response from the model
    response = get_chat_response(message, history_langchain_format)

    return response
    
# Define the introductory content
intro_content = """
# Course Recommendation Bot
This bot helps you find **free courses related to python, data science and machine learning** available on [Analytics Vidhya](https://www.analyticsvidhya.com/).
You can ask any questions related to these courses.

For example:
- "What are the free courses on Python?"
- "Do you have any courses on data science?"
"""

# Build the Gradio interface
with gr.Blocks() as demo:
    gr.Markdown(intro_content)  # Display introductory content
    chatbot = gr.ChatInterface(predict, type="messages")  # Chat interface

demo.launch(share=False, ssr_mode=False)