alx-d commited on
Commit
86c024b
·
1 Parent(s): 6306852

Upload folder using huggingface_hub

Browse files
.gitattributes CHANGED
@@ -33,3 +33,6 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ longevity_books/Hallmarks_of_aging_2013_Cell.pdf filter=lfs diff=lfs merge=lfs -text
37
+ longevity_books/William-Davis-MD-Super-Gut-A-Fou.pdf filter=lfs diff=lfs merge=lfs -text
38
+ longevity_books/examine_healthy_aging_supplement_guide_2020_09_20.pdf filter=lfs diff=lfs merge=lfs -text
README.md CHANGED
@@ -1,12 +1,6 @@
1
  ---
2
- title: Longevity
3
- emoji: 🏢
4
- colorFrom: gray
5
- colorTo: yellow
6
  sdk: gradio
7
  sdk_version: 3.39.0
8
- app_file: app.py
9
- pinned: false
10
  ---
11
-
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: longevity
3
+ app_file: longevity.py
 
 
4
  sdk: gradio
5
  sdk_version: 3.39.0
 
 
6
  ---
 
 
longevity.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from llama_index import SimpleDirectoryReader, LLMPredictor, PromptHelper, StorageContext, ServiceContext, GPTVectorStoreIndex, load_index_from_storage
2
+ from langchain.chat_models import ChatOpenAI
3
+ import gradio as gr
4
+ import sys
5
+ import os
6
+ import openai
7
+ from ratelimit import limits, sleep_and_retry
8
+
9
+ # fixing bugs
10
+ # 1. open ai key: https://stackoverflow.com/questions/76425556/tenacity-retryerror-retryerrorfuture-at-0x7f89bc35eb90-state-finished-raised
11
+ # 2. rate limit error in lang_chain default version - install langchain==0.0.188. https://github.com/jerryjliu/llama_index/issues/924
12
+ # 3. added true Config variable in langchain: https://github.com/pydantic/pydantic/issues/3320
13
+
14
+
15
+ os.environ["OPENAI_API_KEY"] = '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'
16
+ openai.api_key = os.environ["OPENAI_API_KEY"]
17
+
18
+ # Define the rate limit for API calls (requests per second)
19
+ RATE_LIMIT = 3
20
+
21
+ # Implement the rate limiting decorator
22
+ @sleep_and_retry
23
+ @limits(calls=RATE_LIMIT, period=1)
24
+ def create_service_context():
25
+
26
+ #constraint parameters
27
+ max_input_size = 4096
28
+ num_outputs = 512
29
+ max_chunk_overlap = 20
30
+ chunk_size_limit = 600
31
+
32
+ #allows the user to explicitly set certain constraint parameters
33
+ prompt_helper = PromptHelper(max_input_size, num_outputs, max_chunk_overlap, chunk_size_limit=chunk_size_limit)
34
+
35
+ #LLMPredictor is a wrapper class around LangChain's LLMChain that allows easy integration into LlamaIndex
36
+ llm_predictor = LLMPredictor(llm=ChatOpenAI(temperature=0.5, model_name="gpt-3.5-turbo", max_tokens=num_outputs))
37
+
38
+ #constructs service_context
39
+ service_context = ServiceContext.from_defaults(llm_predictor=llm_predictor, prompt_helper=prompt_helper)
40
+ return service_context
41
+
42
+
43
+ # Implement the rate limiting decorator
44
+ @sleep_and_retry
45
+ @limits(calls=RATE_LIMIT, period=1)
46
+ def data_ingestion_indexing(directory_path):
47
+
48
+ #loads data from the specified directory path
49
+ documents = SimpleDirectoryReader(directory_path).load_data()
50
+
51
+ #when first building the index
52
+ index = GPTVectorStoreIndex.from_documents(
53
+ documents, service_context=create_service_context()
54
+ )
55
+
56
+ #persist index to disk, default "storage" folder
57
+ index.storage_context.persist()
58
+
59
+ return index
60
+
61
+ def data_querying(input_text):
62
+
63
+ #rebuild storage context
64
+ storage_context = StorageContext.from_defaults(persist_dir="./storage")
65
+
66
+ #loads index from storage
67
+ index = load_index_from_storage(storage_context, service_context=create_service_context())
68
+
69
+ #queries the index with the input text
70
+ response = index.as_query_engine().query(input_text)
71
+
72
+ return response.response
73
+
74
+ iface = gr.Interface(fn=data_querying,
75
+ inputs=gr.components.Textbox(lines=7, label="Enter your question"),
76
+ outputs="text",
77
+ title="Longevity GPT 0.1 pre alpha")
78
+
79
+ #passes in data directory
80
+ index = data_ingestion_indexing("longevity")
81
+ iface.launch(share=True)
longevity_books/Hallmarks_of_aging_2013_Cell.pdf ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:782302a8436b73248c5865d34241187f5b71184a237373d5f4408438f66eccae
3
+ size 2947444
longevity_books/Nutrition_During_and_After_Cancer_Treatm.pdf ADDED
Binary file (214 kB). View file
 
longevity_books/William-Davis-MD-Super-Gut-A-Fou.pdf ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a3b9516183065316bed5ffeb3dc1939b444269f9139c45ac20c77152e1769e3b
3
+ size 4013849
longevity_books/examine_healthy_aging_supplement_guide_2020_09_20.pdf ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:c71dccaa1e0f5b7ac70e25060c77ef53fdda3b4d45b3408c1c5fbe303d4d3848
3
+ size 2440475
longevity_books/shopping-list.pdf ADDED
Binary file (399 kB). View file
 
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ langchain==0.0.188
2
+ openai
3
+ llama_index==0.6.12
4
+ pypdf
5
+ PyCryptodome
6
+ ratelimit