Spaces:
Sleeping
Sleeping
Commit
•
f9f8030
1
Parent(s):
cfb14fd
new zephyr model
Browse files
app.py
CHANGED
@@ -1,5 +1,38 @@
|
|
1 |
import gradio as gr
|
2 |
import requests
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
def authenticate(username, password):
|
5 |
if username == "Gribouille" and password == "A jamais les premiers":
|
@@ -8,8 +41,7 @@ def authenticate(username, password):
|
|
8 |
return False
|
9 |
|
10 |
def predict(query):
|
11 |
-
|
12 |
-
return str(response.text)
|
13 |
|
14 |
iface = gr.Interface(fn=predict, inputs=["text"], outputs="text")
|
15 |
iface.launch(auth=authenticate)
|
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
+
import os
|
4 |
+
from pathlib import Path
|
5 |
+
from llama_index.embeddings import HuggingFaceEmbedding, VoyageEmbedding
|
6 |
+
from llama_index import (load_index_from_storage, ServiceContext, StorageContext, VectorStoreIndex)
|
7 |
+
from llama_index import download_loader, SimpleDirectoryReader
|
8 |
+
from llama_index.retrievers import RecursiveRetriever
|
9 |
+
from llama_index.query_engine import RetrieverQueryEngine
|
10 |
+
from llama_index.llms import Anyscale
|
11 |
+
|
12 |
+
# Define the inference model
|
13 |
+
# llm = Anyscale(model="mistralai/Mistral-7B-Instruct-v0.1", api_key=os.getenv("ANYSCALE_API_KEY"))
|
14 |
+
llm = Anyscale(model="HuggingFaceH4/zephyr-7b-beta", api_key=os.getenv("ANYSCALE_API_KEY"))
|
15 |
+
# Define the embedding model used to embed the query.
|
16 |
+
# query_embed_model = HuggingFaceEmbedding(model_name="BAAI/bge-base-en-v1.5")
|
17 |
+
embed_model = VoyageEmbedding(model_name="voyage-01", voyage_api_key=os.getenv("VOYAGE_API_KEY"))
|
18 |
+
service_context = ServiceContext.from_defaults(llm=llm, embed_model=embed_model)
|
19 |
+
|
20 |
+
if "index" in os.listdir():
|
21 |
+
storage_context = StorageContext.from_defaults(persist_dir=Path("./index"))
|
22 |
+
else:
|
23 |
+
dir_reader = SimpleDirectoryReader(Path('./docs'))
|
24 |
+
documents = dir_reader.load_data()
|
25 |
+
index = VectorStoreIndex.from_documents(documents, service_context=service_context)
|
26 |
+
index.storage_context.persist(Path('./index'))
|
27 |
+
storage_context = StorageContext.from_defaults(persist_dir=Path("./index"))
|
28 |
+
|
29 |
+
# Load the vector stores that were created earlier.
|
30 |
+
index = load_index_from_storage(storage_context=storage_context, service_context=service_context)
|
31 |
+
|
32 |
+
# Define query engine:
|
33 |
+
index_engine = index.as_retriever(similarity_top_k=4)
|
34 |
+
index_retriever = RecursiveRetriever("vector",retriever_dict={"vector": index_engine})
|
35 |
+
query_engine = RetrieverQueryEngine.from_args(index_retriever, service_context=service_context)
|
36 |
|
37 |
def authenticate(username, password):
|
38 |
if username == "Gribouille" and password == "A jamais les premiers":
|
|
|
41 |
return False
|
42 |
|
43 |
def predict(query):
|
44 |
+
return str(query_engine.query(query))
|
|
|
45 |
|
46 |
iface = gr.Interface(fn=predict, inputs=["text"], outputs="text")
|
47 |
iface.launch(auth=authenticate)
|