Update app.py
Browse files
app.py
CHANGED
|
@@ -1,62 +1,43 @@
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from g4f import Provider, models
|
| 3 |
-
from langchain.llms.base import LLM
|
| 4 |
-
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
|
| 5 |
import asyncio
|
| 6 |
import nest_asyncio
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
from llama_index
|
| 12 |
-
from
|
| 13 |
-
from
|
| 14 |
from g4f import Provider, models
|
| 15 |
from langchain.llms.base import LLM
|
| 16 |
-
from llama_index.llms import LangChainLLM
|
| 17 |
-
from
|
|
|
|
| 18 |
nest_asyncio.apply()
|
| 19 |
-
from huggingface_hub import hf_hub_download
|
| 20 |
|
| 21 |
-
|
| 22 |
-
model_basename = "ggml-model-q4_0.bin" # the model is in bin format
|
| 23 |
|
| 24 |
-
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
| 28 |
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
num_output=256,
|
| 37 |
-
chunk_overlap_ratio=0.1,
|
| 38 |
-
chunk_size_limit=None
|
| 39 |
-
)
|
| 40 |
-
"""
|
| 41 |
-
from langchain_g4f import G4FLLM
|
| 42 |
-
llm = LLM = G4FLLM(
|
| 43 |
-
model=models.gpt_35_turbo,
|
| 44 |
-
provider=Provider.Acytoo,)
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
llm = LangChainLLM(llm=llm)
|
| 48 |
-
|
| 49 |
-
service_context = ServiceContext.from_defaults(llm=llm,
|
| 50 |
-
embed_model=embed_model)
|
| 51 |
-
|
| 52 |
-
documents = SimpleDirectoryReader("data").load_data()
|
| 53 |
-
index = VectorStoreIndex.from_documents(documents, service_context=service_context)
|
| 54 |
-
|
| 55 |
-
async def main(question):
|
| 56 |
-
query_engine = index.as_query_engine(service_context=service_context)
|
| 57 |
-
response = query_engine.query(question)
|
| 58 |
-
print(response)
|
| 59 |
return response
|
| 60 |
|
| 61 |
-
iface = Interface(fn=main, inputs="text", outputs="text")
|
| 62 |
-
iface.launch()
|
|
|
|
| 1 |
+
import logging
|
| 2 |
+
import sys
|
| 3 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 4 |
import asyncio
|
| 5 |
import nest_asyncio
|
| 6 |
+
|
| 7 |
+
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
|
| 8 |
+
logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))
|
| 9 |
+
|
| 10 |
+
from llama_index import VectorStoreIndex, SimpleDirectoryReader, ServiceContext
|
| 11 |
+
from llama_index.llms import HuggingFaceLLM
|
| 12 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
| 13 |
from g4f import Provider, models
|
| 14 |
from langchain.llms.base import LLM
|
| 15 |
+
from llama_index.llms import LangChainLLM
|
| 16 |
+
from langchain_g4f import G4FLLM
|
| 17 |
+
|
| 18 |
nest_asyncio.apply()
|
|
|
|
| 19 |
|
| 20 |
+
documents = SimpleDirectoryReader('data').load_data()
|
|
|
|
| 21 |
|
| 22 |
+
embed_model = HuggingFaceEmbeddings(
|
| 23 |
+
model_name="sentence-transformers/all-mpnet-base-v2"
|
| 24 |
+
)
|
| 25 |
|
| 26 |
+
async def main(query):
|
| 27 |
+
llm: LLM = G4FLLM(
|
| 28 |
+
model=models.gpt_35_turbo,
|
| 29 |
+
provider=Provider.DeepAi,
|
| 30 |
+
)
|
| 31 |
|
| 32 |
+
llm = LangChainLLM(llm=llm)
|
| 33 |
|
| 34 |
+
service_context = ServiceContext.from_defaults(chunk_size=512, llm=llm, embed_model=embed_model)
|
| 35 |
+
|
| 36 |
+
index = VectorStoreIndex.from_documents(documents, service_context=service_context)
|
| 37 |
+
|
| 38 |
+
query_engine = index.as_query_engine()
|
| 39 |
+
response = query_engine.query(query)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
return response
|
| 41 |
|
| 42 |
+
iface = gr.Interface(fn=main, inputs="text", outputs="text")
|
| 43 |
+
iface.launch()
|