Spaces:
Runtime error
Runtime error
ishaan-mital
commited on
Commit
·
8e3e8e3
1
Parent(s):
077ae05
initial commit
Browse files
app.py
CHANGED
@@ -9,72 +9,67 @@ from langchain.embeddings.huggingface import HuggingFaceEmbeddings
|
|
9 |
import time
|
10 |
|
11 |
API_URL = "https://api-inference.huggingface.co/models/HuggingFaceH4/zephyr-7b-beta"
|
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 |
-
payload = {"input":
|
|
|
|
|
|
|
|
|
54 |
response = requests.post(API_URL, headers=headers, json=payload)
|
55 |
return response.json() # Adjust as needed based on your API response format
|
56 |
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
def __init__(self, api_func):
|
62 |
-
self.api_func = api_func
|
63 |
-
|
64 |
-
def run(self, input_text):
|
65 |
-
return self.api_func(input_text)
|
66 |
-
|
67 |
-
api_runnable = APIRunnable(api_func=call_llm_api)
|
68 |
-
|
69 |
-
rag_pipeline = RetrievalQA.from_chain_type(
|
70 |
-
llm=api_runnable, chain_type='stuff',
|
71 |
-
retriever=vectorstore.as_retriever()
|
72 |
-
)
|
73 |
|
74 |
|
75 |
def main(question):
|
76 |
-
|
77 |
-
|
|
|
|
|
|
|
78 |
|
79 |
demo = gr.Interface(main, inputs = "text", outputs = "text")
|
80 |
|
|
|
9 |
import time
|
10 |
|
11 |
API_URL = "https://api-inference.huggingface.co/models/HuggingFaceH4/zephyr-7b-beta"
|
12 |
+
headers = {"Authorization": f"Bearer {os.environ.get('API_KEY')}"}
|
13 |
+
retrieval = Client("https://ishaan-mital-ncert-helper-vector-db.hf.space/--replicas/149bg26k5/")
|
14 |
+
|
15 |
+
# embed_model_id = 'sentence-transformers/all-MiniLM-L6-v2'
|
16 |
+
# # device = f'cuda:{cuda.current_device()}' if cuda.is_available() else 'cpu'
|
17 |
+
|
18 |
+
# embed_model = HuggingFaceEmbeddings(
|
19 |
+
# model_name=embed_model_id,
|
20 |
+
# # model_kwargs={'device': device},
|
21 |
+
# # encode_kwargs={'device': device, 'batch_size': 32}
|
22 |
+
# )
|
23 |
+
|
24 |
+
|
25 |
+
# pinecone.init(
|
26 |
+
# api_key=os.environ.get('PINECONE_API_KEY'),
|
27 |
+
# environment=os.environ.get('PINECONE_ENVIRONMENT')
|
28 |
+
# )
|
29 |
+
|
30 |
+
# index_name = 'llama-rag'
|
31 |
+
# index = pinecone.Index(index_name)
|
32 |
+
# text_field = 'text' # field in metadata that contains text content
|
33 |
+
# docs = [
|
34 |
+
# "this is one document",
|
35 |
+
# "and another document"
|
36 |
+
# ]
|
37 |
+
|
38 |
+
# embeddings = embed_model.embed_documents(docs)
|
39 |
+
# if index_name not in pinecone.list_indexes():
|
40 |
+
# pinecone.create_index(
|
41 |
+
# index_name,
|
42 |
+
# dimension=len(embeddings[0]),
|
43 |
+
# metric='cosine'
|
44 |
+
# )
|
45 |
+
# # wait for index to finish initialization
|
46 |
+
# while not pinecone.describe_index(index_name).status['ready']:
|
47 |
+
# time.sleep(1)
|
48 |
+
# vectorstore = Pinecone(
|
49 |
+
# index, embed_model.embed_query, text_field
|
50 |
+
# )
|
51 |
+
|
52 |
+
def call_llm_api(input_text,context):
|
53 |
+
payload = {"input": {
|
54 |
+
"question": input_text,
|
55 |
+
"context": context
|
56 |
+
}
|
57 |
+
}
|
58 |
response = requests.post(API_URL, headers=headers, json=payload)
|
59 |
return response.json() # Adjust as needed based on your API response format
|
60 |
|
61 |
+
# rag_pipeline = RetrievalQA.from_chain_type(
|
62 |
+
# llm=call_llm_api, chain_type='stuff',
|
63 |
+
# retriever=vectorstore.as_retriever()
|
64 |
+
# )
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
|
66 |
|
67 |
def main(question):
|
68 |
+
# return rag_pipeline(question)
|
69 |
+
context = retrieval.predict(question, api_name = "/predict")
|
70 |
+
answer=call_llm_api(question,context)
|
71 |
+
chatbot = answer[1]
|
72 |
+
return answer[1][0][1]
|
73 |
|
74 |
demo = gr.Interface(main, inputs = "text", outputs = "text")
|
75 |
|