Stéphane G commited on
Commit
8940849
·
1 Parent(s): 8bdf133

initial commir - add app files

Browse files
Files changed (6) hide show
  1. .gitignore +1 -0
  2. Dockerfile +16 -0
  3. README.md +3 -3
  4. code_qa_app.py +167 -0
  5. requirements.txt +8 -0
  6. scripts/dev_up.sh +8 -0
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ .DS_Store
Dockerfile ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.9
2
+
3
+ WORKDIR /code
4
+
5
+ COPY ./requirements.txt /code/requirements.txt
6
+ RUN python3 -m pip install --no-cache-dir --upgrade pip
7
+ RUN python3 -m pip install --no-cache-dir --upgrade -r /code/requirements.txt
8
+
9
+ COPY . .
10
+
11
+ CMD ["panel", "serve", "/code/code_qa_app.py", "--address", "0.0.0.0", "--port", "7860", "--allow-websocket-origin", "spqw-org-code-qa.hf.space", "--allow-websocket-origin", "0.0.0.0:7860"]
12
+
13
+ RUN mkdir /.cache
14
+ RUN chmod 777 /.cache
15
+ RUN mkdir .chroma
16
+ RUN chmod 777 .chroma
README.md CHANGED
@@ -1,10 +1,10 @@
1
  ---
2
- title: Code Qa
3
- emoji: 😻
4
  colorFrom: gray
5
  colorTo: gray
6
  sdk: docker
7
  pinned: false
8
  ---
9
 
10
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: Code QA
3
+ emoji: 🤖
4
  colorFrom: gray
5
  colorTo: gray
6
  sdk: docker
7
  pinned: false
8
  ---
9
 
10
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
code_qa_app.py ADDED
@@ -0,0 +1,167 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!pip install langchain openai chromadb tiktoken panel gitpython
2
+
3
+ import os
4
+ from langchain.chains import RetrievalQA
5
+ from langchain.llms import OpenAI
6
+ from langchain.document_loaders import GitLoader
7
+ from langchain.indexes import VectorstoreIndexCreator
8
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
9
+ from langchain.embeddings import OpenAIEmbeddings
10
+ from langchain.vectorstores import Chroma
11
+ import panel as pn
12
+
13
+ # UI configuration
14
+ pn.extension(
15
+ 'texteditor',
16
+ template="bootstrap",
17
+ sizing_mode='stretch_width'
18
+ )
19
+
20
+ pn.state.template.param.update(
21
+ main_max_width="690px",
22
+ header_background="#F08080",
23
+ )
24
+
25
+ # User inputs
26
+ url_repo = pn.widgets.TextInput(
27
+ value="",
28
+ placeholder="Enter your GitHub repo URL here...",
29
+ width=300
30
+ )
31
+
32
+ branch_repo = pn.widgets.TextInput(
33
+ value="",
34
+ placeholder="Enter the GitHub repo branch here...",
35
+ width=300
36
+ )
37
+
38
+ openaikey = pn.widgets.PasswordInput(
39
+ value="",
40
+ placeholder="Enter your OpenAI API Key here...",
41
+ width=300
42
+ )
43
+
44
+ prompt = pn.widgets.TextEditor(
45
+ value="",
46
+ placeholder="Enter your questions here...",
47
+ height=160,
48
+ toolbar=False
49
+ )
50
+
51
+ run_button = pn.widgets.Button(name="Run!")
52
+
53
+ select_k = pn.widgets.IntSlider(
54
+ name="Number of relevant chunks",
55
+ start=1,
56
+ end=5,
57
+ step=1,
58
+ value=2
59
+ )
60
+
61
+ select_chain_type = pn.widgets.RadioButtonGroup(
62
+ name='Chain type',
63
+ options=['stuff', 'map_reduce', "refine", "map_rerank"]
64
+ )
65
+
66
+ # Question answering function
67
+ def qa(url_repo, branch, query, chain_type, k):
68
+ # load data
69
+ loader = GitLoader(
70
+ clone_url=url_repo,
71
+ repo_path=".cache",
72
+ branch=branch,
73
+ file_filter=lambda file_path: file_path.endswith(".swift") or file_path.endswith(".h") or file_path.endswith(".m")
74
+ )
75
+ documents = loader.load()
76
+
77
+ # split documents into chunks
78
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
79
+ texts = text_splitter.split_documents(documents)
80
+
81
+ # select which embeddings to use
82
+ embeddings = OpenAIEmbeddings()
83
+
84
+ # create the vectorstore to use as the index
85
+ db = Chroma.from_documents(texts, embeddings)
86
+
87
+ # expose this index in a retriever interface
88
+ retriever = db.as_retriever(search_type="similarity", search_kwargs={"k": k})
89
+
90
+ # create a chain to answer questions
91
+ qa = RetrievalQA.from_chain_type(
92
+ llm=OpenAI(),
93
+ chain_type=chain_type,
94
+ retriever=retriever,
95
+ return_source_documents=True
96
+ )
97
+
98
+ result = qa({"query": query})
99
+ print(result['result'])
100
+ return result
101
+
102
+
103
+ convos = [] # store all panel objects in a list
104
+
105
+ def qa_result(_):
106
+ os.environ["OPENAI_API_KEY"] = openaikey.value
107
+
108
+ prompt_text = prompt.value
109
+ if prompt_text:
110
+ result = qa(url_repo=url_repo.value, branch=branch_repo.value, query=prompt_text, chain_type=select_chain_type.value, k=select_k.value)
111
+ convos.extend([
112
+ pn.Row(
113
+ pn.panel("\U0001F60A", width=10),
114
+ prompt_text,
115
+ width=600
116
+ ),
117
+ pn.Row(
118
+ pn.panel("\U0001F916", width=10),
119
+ pn.Column(
120
+ result["result"],
121
+ "Relevant source text:",
122
+ pn.pane.Markdown('\n--------------------------------------------------------------------\n'.join(doc.page_content for doc in result["source_documents"]))
123
+ )
124
+ )
125
+ ])
126
+ #return convos
127
+ return pn.Column(*convos, margin=15, width=575, min_height=400)
128
+
129
+ qa_interactive = pn.panel(
130
+ pn.bind(qa_result, run_button),
131
+ loading_indicator=True,
132
+ )
133
+
134
+ # Output
135
+ output = pn.WidgetBox('*Output will show up here:*', qa_interactive, width=630, scroll=True)
136
+
137
+ # Layout
138
+ widgets = pn.Row(
139
+ pn.Column(
140
+ prompt,
141
+ run_button,
142
+ margin=5
143
+ ),
144
+ pn.Card(
145
+ "Chain type:",
146
+ pn.Column(
147
+ select_chain_type,
148
+ select_k
149
+ ),
150
+ title="Advanced settings", margin=10
151
+ ), width=600
152
+ )
153
+
154
+ pn.Column(
155
+ pn.pane.Markdown("""
156
+ ## \U0001F60A - Question Answering with your Github repo (swift / objc)
157
+
158
+ 1) Enter the url of a github repo and a branch\n
159
+ 2) Enter OpenAI API key. This costs $. Set up billing at [OpenAI](https://platform.openai.com/account).\n
160
+ 3) Type a question and click "Run".\n
161
+
162
+ """),
163
+ pn.Column(url_repo, branch_repo, openaikey),
164
+ output,
165
+ widgets
166
+
167
+ ).servable()
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ langchain
2
+ openai
3
+ chromadb
4
+ pypdf
5
+ tiktoken
6
+ panel
7
+ notebook
8
+ gitpython
scripts/dev_up.sh ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env bash
2
+
3
+ set -o errexit
4
+ set -o pipefail
5
+ set -o nounset
6
+ set -o xtrace
7
+
8
+ panel serve code_qa_app.py --address 0.0.0.0 --port 7860 --allow-websocket-origin 0.0.0.0:7860