Docfile commited on
Commit
ea114c9
·
1 Parent(s): 06996d5

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -0
app.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+
4
+ from llama_index import (
5
+ ServiceContext,
6
+ SimpleDirectoryReader,
7
+ StorageContext,
8
+ VectorStoreIndex,
9
+ set_global_service_context,
10
+ )
11
+ from llama_index.llms import Gemini
12
+ from llama_index.embeddings import GeminiEmbedding
13
+
14
+ model_name = "models/embedding-001"
15
+
16
+
17
+ llm = Gemini()
18
+ embed_model = GeminiEmbedding(
19
+ model_name=model_name, api_key=GOOGLE_API_KEY, title="this is a document"
20
+ )
21
+ # Reads pdfs at "./" path
22
+ documents = (
23
+ SimpleDirectoryReader(
24
+ input_dir = './',
25
+ required_exts = [".pdf"])
26
+ .load_data()
27
+ )
28
+
29
+ # ServiceContext is a bundle of commonly used
30
+ # resources used during the indexing and
31
+ # querying stage
32
+ service_context = (
33
+ ServiceContext
34
+ .from_defaults(
35
+ llm=llm,
36
+ embed_model=embed_model,
37
+ chunk_size=545
38
+ )
39
+ )
40
+ set_global_service_context(service_context)
41
+ print("node passer11")
42
+ # Node represents a “chunk” of a source Document
43
+ nodes = (
44
+ service_context
45
+ .node_parser
46
+ .get_nodes_from_documents(documents)
47
+ )
48
+ print("node passer")
49
+ # offers core abstractions around storage of Nodes,
50
+ # indices, and vectors
51
+ storage_context = StorageContext.from_defaults()
52
+ storage_context.docstore.add_documents(nodes)
53
+ print("node passer")
54
+ # Create the vectorstore index
55
+ index = (
56
+ VectorStoreIndex
57
+ .from_documents(
58
+ documents,
59
+ storage_context=storage_context,
60
+ llm=llm
61
+ )
62
+ )
63
+ print("node passer")
64
+ query_engine = index.as_query_engine()
65
+
66
+ # Query the index
67
+
68
+
69
+ def greet(name):
70
+ response = query_engine.query(name)
71
+ return response
72
+
73
+ iface = gr.Interface(fn=greet, inputs="text", outputs="text")
74
+ iface.launch()