Spaces:
Paused
Paused
Franc v0.9
Browse files- .idea/.gitignore +8 -0
- .idea/franc-v0.9.iml +8 -0
- .idea/inspectionProfiles/profiles_settings.xml +6 -0
- .idea/misc.xml +4 -0
- .idea/modules.xml +8 -0
- .idea/vcs.xml +6 -0
- main.py +115 -0
- requirements.txt +8 -0
.idea/.gitignore
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Default ignored files
|
| 2 |
+
/shelf/
|
| 3 |
+
/workspace.xml
|
| 4 |
+
# Editor-based HTTP Client requests
|
| 5 |
+
/httpRequests/
|
| 6 |
+
# Datasource local storage ignored files
|
| 7 |
+
/dataSources/
|
| 8 |
+
/dataSources.local.xml
|
.idea/franc-v0.9.iml
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
| 2 |
+
<module type="PYTHON_MODULE" version="4">
|
| 3 |
+
<component name="NewModuleRootManager">
|
| 4 |
+
<content url="file://$MODULE_DIR$" />
|
| 5 |
+
<orderEntry type="inheritedJdk" />
|
| 6 |
+
<orderEntry type="sourceFolder" forTests="false" />
|
| 7 |
+
</component>
|
| 8 |
+
</module>
|
.idea/inspectionProfiles/profiles_settings.xml
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<component name="InspectionProjectProfileManager">
|
| 2 |
+
<settings>
|
| 3 |
+
<option name="USE_PROJECT_PROFILE" value="false" />
|
| 4 |
+
<version value="1.0" />
|
| 5 |
+
</settings>
|
| 6 |
+
</component>
|
.idea/misc.xml
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
| 2 |
+
<project version="4">
|
| 3 |
+
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.11" project-jdk-type="Python SDK" />
|
| 4 |
+
</project>
|
.idea/modules.xml
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
| 2 |
+
<project version="4">
|
| 3 |
+
<component name="ProjectModuleManager">
|
| 4 |
+
<modules>
|
| 5 |
+
<module fileurl="file://$PROJECT_DIR$/.idea/franc-v0.9.iml" filepath="$PROJECT_DIR$/.idea/franc-v0.9.iml" />
|
| 6 |
+
</modules>
|
| 7 |
+
</component>
|
| 8 |
+
</project>
|
.idea/vcs.xml
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
| 2 |
+
<project version="4">
|
| 3 |
+
<component name="VcsDirectoryMappings">
|
| 4 |
+
<mapping directory="" vcs="Git" />
|
| 5 |
+
</component>
|
| 6 |
+
</project>
|
main.py
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import pinecone
|
| 5 |
+
from langchain import PromptTemplate
|
| 6 |
+
from langchain.chains import RetrievalQA
|
| 7 |
+
from langchain.embeddings.huggingface import HuggingFaceEmbeddings
|
| 8 |
+
from langchain.llms import HuggingFaceEndpoint
|
| 9 |
+
from langchain.vectorstores import Pinecone
|
| 10 |
+
from torch import cuda
|
| 11 |
+
|
| 12 |
+
LLAMA_2_7B_CHAT_HF_FRANC_V0_9 = os.environ.get("LLAMA_2_7B_CHAT_HF_FRANC_V0_9")
|
| 13 |
+
HUGGING_FACE_HUB_TOKEN = os.environ.get("HUGGING_FACE_HUB_TOKEN")
|
| 14 |
+
PINECONE_API_KEY = os.environ.get('PINECONE_API_KEY')
|
| 15 |
+
PINECONE_ENVIRONMENT = os.environ.get('PINECONE_ENVIRONMENT')
|
| 16 |
+
|
| 17 |
+
# Set up Pinecone vector store
|
| 18 |
+
|
| 19 |
+
pinecone.init(
|
| 20 |
+
api_key=PINECONE_API_KEY,
|
| 21 |
+
environment=PINECONE_ENVIRONMENT
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
index_name = 'stadion-6237'
|
| 25 |
+
index = pinecone.Index(index_name)
|
| 26 |
+
embedding_model_id = 'sentence-transformers/paraphrase-mpnet-base-v2'
|
| 27 |
+
device = f'cuda:{cuda.current_device()}' if cuda.is_available() else 'cpu'
|
| 28 |
+
embedding_model = HuggingFaceEmbeddings(
|
| 29 |
+
model_name=embedding_model_id,
|
| 30 |
+
model_kwargs={'device': device},
|
| 31 |
+
encode_kwargs={'device': device, 'batch_size': 32}
|
| 32 |
+
)
|
| 33 |
+
text_key = 'text'
|
| 34 |
+
|
| 35 |
+
vector_store = Pinecone(
|
| 36 |
+
index, embedding_model.embed_query, text_key
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
B_INST, E_INST = "[INST] ", " [/INST]"
|
| 40 |
+
B_SYS, E_SYS = "<<SYS>>\n", "\n<</SYS>>\n\n"
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def get_prompt_template(instruction, system_prompt):
|
| 44 |
+
system_prompt = B_SYS + system_prompt + E_SYS
|
| 45 |
+
prompt_template = B_INST + system_prompt + instruction + E_INST
|
| 46 |
+
return prompt_template
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
template = get_prompt_template(
|
| 50 |
+
"""Use the following pieces of context to answer the question at the end.
|
| 51 |
+
|
| 52 |
+
{context}
|
| 53 |
+
|
| 54 |
+
Question: {question}
|
| 55 |
+
Helpful Answer:""",
|
| 56 |
+
"""Your name is Franc.
|
| 57 |
+
You are a running coach and exercise physiologist.
|
| 58 |
+
You communicate in the style of Hal Higdon.
|
| 59 |
+
Your answers are always 512-character long or less.
|
| 60 |
+
If you don't know the answer to a question, please don't share false information."""
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
endpoint_url = (
|
| 64 |
+
LLAMA_2_7B_CHAT_HF_FRANC_V0_9
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
llm = HuggingFaceEndpoint(
|
| 68 |
+
endpoint_url=endpoint_url,
|
| 69 |
+
huggingfacehub_api_token=HUGGING_FACE_HUB_TOKEN,
|
| 70 |
+
task="text-generation",
|
| 71 |
+
model_kwargs={
|
| 72 |
+
"max_new_tokens": 512,
|
| 73 |
+
"temperature": 0.3,
|
| 74 |
+
"repetition_penalty": 1.2,
|
| 75 |
+
"return_full_text": True,
|
| 76 |
+
}
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
rag_chain = RetrievalQA.from_chain_type(
|
| 80 |
+
llm=llm,
|
| 81 |
+
chain_type='stuff',
|
| 82 |
+
retriever=vector_store.as_retriever(),
|
| 83 |
+
chain_type_kwargs={
|
| 84 |
+
"prompt": PromptTemplate(
|
| 85 |
+
template=template,
|
| 86 |
+
input_variables=["context", "question"],
|
| 87 |
+
),
|
| 88 |
+
},
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
def generate(message):
|
| 93 |
+
return rag_chain(message)
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
gr.ChatInterface(
|
| 97 |
+
generate,
|
| 98 |
+
title="Franc v0.1",
|
| 99 |
+
description="Meet Franc, a running coach who has become the go-to expert for runners, whether they are just "
|
| 100 |
+
"lacing up their shoes for the first time or seasoned marathoners. With his deep understanding of "
|
| 101 |
+
"exercise physiology, Franc answers all sorts of questions, guiding individuals on their unique "
|
| 102 |
+
"running journeys. But don't be fooled – even with his vast knowledge, Franc has been known to make "
|
| 103 |
+
"mistakes, and his advice should always be taken with a grain of salt. What sets him apart from other "
|
| 104 |
+
"coaches, however, is his approach, dedication, and a surprising revelation: Franc is actually a "
|
| 105 |
+
"fine-tuned LLM (Language Model), blending technology with human-like empathy to help his runners "
|
| 106 |
+
"achieve their goals.",
|
| 107 |
+
examples=[
|
| 108 |
+
"What's VO2max?"
|
| 109 |
+
],
|
| 110 |
+
cache_examples=True,
|
| 111 |
+
theme=gr.themes.Soft(),
|
| 112 |
+
submit_btn="Ask Franc",
|
| 113 |
+
retry_btn="Do better, Franc!",
|
| 114 |
+
autofocus=True,
|
| 115 |
+
).queue().launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
huggingface-hub
|
| 3 |
+
langchain
|
| 4 |
+
pinecone-client
|
| 5 |
+
sentence-transformers
|
| 6 |
+
text-generation
|
| 7 |
+
torch
|
| 8 |
+
transformers
|