Pulkit-exe commited on
Commit
e5224e4
·
1 Parent(s): 13e8efb

Adding app and requirements

Browse files
Files changed (2) hide show
  1. gradio_app.py +98 -0
  2. requirements.txt +5 -0
gradio_app.py ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sentence_transformers import SentenceTransformer
2
+ from langchain.prompts import ChatPromptTemplate
3
+ from langchain_huggingface import HuggingFaceEndpoint
4
+ from langchain.schema import AIMessage, HumanMessage
5
+ from langchain_chroma import Chroma
6
+ import gradio as gr
7
+
8
+ # Load environment variables
9
+ CHROMA_PATH = "chroma"
10
+
11
+ # Hugging Face API setup
12
+ repo_id = "Qwen/Qwen2.5-Coder-32B-Instruct"
13
+
14
+ PROMPT_TEMPLATE = """
15
+ Answer the question based on the context provided. If no relevant information is found, state so.
16
+
17
+ Context:
18
+ {context}
19
+
20
+ Question:
21
+ {question}
22
+
23
+ Answer:
24
+ """
25
+
26
+ # Initialize the local embedding model
27
+ embedding_model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
28
+
29
+
30
+ class LocalEmbeddingFunction:
31
+ def embed_documents(self, texts):
32
+ # Generate embeddings for a list of texts
33
+ embeddings = embedding_model.encode(texts)
34
+ return embeddings.tolist() if hasattr(embeddings, 'tolist') else embeddings
35
+
36
+ def embed_query(self, query):
37
+ # Generate an embedding for a single query string
38
+ query_embedding = embedding_model.encode(query)
39
+ return query_embedding.tolist() if hasattr(query_embedding, 'tolist') else query_embedding
40
+
41
+
42
+ class LLM:
43
+ llm = HuggingFaceEndpoint(
44
+ repo_id=repo_id,
45
+ temperature=0.2,
46
+ )
47
+
48
+ def generate_response(self, prompt):
49
+ return self.llm.invoke(prompt)
50
+
51
+
52
+ def get_embedding_function():
53
+ return LocalEmbeddingFunction()
54
+
55
+ def get_chat_response(query, history):
56
+ # Retrieve embeddings from the Chroma DB
57
+ embedding_function = get_embedding_function()
58
+ db = Chroma(persist_directory=CHROMA_PATH, embedding_function=embedding_function)
59
+
60
+ # Search the DB
61
+ results = db.similarity_search_with_score(query, k=5)
62
+ context_text = "\n\n---\n\n".join([doc.page_content for doc, _score in results])
63
+
64
+ # Create prompt with context and query
65
+ prompt_template = ChatPromptTemplate.from_template(PROMPT_TEMPLATE)
66
+ prompt = prompt_template.format(context=context_text, question=query)
67
+
68
+ # Generate response using the LLM class and Hugging Face model
69
+ model = LLM()
70
+ response_text = model.generate_response(prompt)
71
+
72
+ # Update the history
73
+ history.append(AIMessage(content = response_text))
74
+
75
+ return response_text
76
+
77
+
78
+ # Gradio Interface
79
+ def predict(message, history):
80
+ # Initialize history if not provided
81
+ history_langchain_format = []
82
+
83
+ for msg in history:
84
+
85
+ if msg['role'] == "user":
86
+ history_langchain_format.append(HumanMessage(content=msg['content']))
87
+
88
+ elif msg['role'] == "assistant":
89
+ history_langchain_format.append(AIMessage(content=msg['content']))
90
+
91
+ history_langchain_format.append(HumanMessage(content=message))
92
+
93
+ # Get response from the model
94
+ response = get_chat_response(message, history_langchain_format)
95
+
96
+ return response
97
+
98
+ gr.ChatInterface(predict, type="messages").launch()
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ sentence-transformers
2
+ langchain
3
+ langchain-huggingface
4
+ gradio
5
+ chromadb