|
1 | 1 | import streamlit as st |
2 | 2 | import os |
3 | 3 | import shutil |
4 | | -import git |
5 | 4 | from streamlit_ace import st_ace |
6 | | -from pymarkdown.api import PyMarkdownApi |
| 5 | +import requests |
| 6 | + |
| 7 | +from streamlit.runtime.scriptrunner import get_script_run_ctx |
7 | 8 |
|
8 | 9 | # App title |
9 | 10 | st.set_page_config(page_title="Readme Generator", layout="wide",page_icon=":material/graphic_eq:") |
|
20 | 21 | hf_token = st.text_input('Enter HuggingFace token:', type='password') |
21 | 22 | os.environ['HF_TOKEN'] = hf_token |
22 | 23 |
|
23 | | - from readme_ready.query import query |
24 | | - from readme_ready.index import index |
25 | | - from readme_ready.types import AutodocRepoConfig, AutodocUserConfig, LLMModels |
| 24 | + ctx = get_script_run_ctx() |
| 25 | + session_id = None |
| 26 | + if ctx: |
| 27 | + session_id = ctx.session_id |
| 28 | + # You can now use session_id for various purposes, e.g., logging or file naming |
| 29 | + print(f"Current Streamlit session ID: {session_id}") |
| 30 | + else: |
| 31 | + print("Not running within a Streamlit context.") |
26 | 32 |
|
27 | 33 | with st.form("my_form"): |
28 | 34 | st.subheader('Model') |
29 | | - options = [ |
30 | | - LLMModels.TINYLLAMA_1p1B_CHAT_GGUF.value, |
31 | | - LLMModels.GOOGLE_GEMMA_2B_INSTRUCT_GGUF.value, |
32 | | - LLMModels.LLAMA2_7B_CHAT_GPTQ.value, |
33 | | - LLMModels.LLAMA2_13B_CHAT_GPTQ.value, |
34 | | - LLMModels.CODELLAMA_7B_INSTRUCT_GPTQ.value, |
35 | | - LLMModels.CODELLAMA_13B_INSTRUCT_GPTQ.value, |
36 | | - LLMModels.LLAMA2_7B_CHAT_HF.value, |
37 | | - LLMModels.LLAMA2_13B_CHAT_HF.value, |
38 | | - LLMModels.CODELLAMA_7B_INSTRUCT_HF.value, |
39 | | - LLMModels.CODELLAMA_13B_INSTRUCT_HF.value, |
40 | | - LLMModels.GOOGLE_GEMMA_2B_INSTRUCT.value, |
41 | | - LLMModels.GOOGLE_GEMMA_7B_INSTRUCT.value, |
42 | | - LLMModels.GOOGLE_CODEGEMMA_2B.value, |
43 | | - LLMModels.GOOGLE_CODEGEMMA_7B_INSTRUCT.value, |
44 | | - ] |
| 35 | + options = requests.get(url="http://127.0.0.1:8000/models").json() |
45 | 36 | llm = st.selectbox('Choose a model', options, key='llm') |
46 | 37 | device = st.selectbox('Choose a device', ["cpu", "auto"], key='device') |
47 | 38 | st.subheader('Parameters') |
|
59 | 50 | submitted = st.form_submit_button("Setup") |
60 | 51 | if submitted: |
61 | 52 | st.toast('Indexing repository...') |
62 | | - try: |
63 | | - repo = git.Repo.clone_from(project_url, project_root) |
64 | | - except: |
65 | | - print('Project already exists.') |
66 | | - |
67 | | - match llm: |
68 | | - case LLMModels.TINYLLAMA_1p1B_CHAT_GGUF.value: |
69 | | - model = LLMModels.TINYLLAMA_1p1B_CHAT_GGUF |
70 | | - case LLMModels.GOOGLE_GEMMA_2B_INSTRUCT_GGUF.value: |
71 | | - model = LLMModels.GOOGLE_GEMMA_2B_INSTRUCT_GGUF |
72 | | - case LLMModels.LLAMA2_7B_CHAT_GPTQ.value: |
73 | | - model = LLMModels.LLAMA2_7B_CHAT_GPTQ |
74 | | - case LLMModels.LLAMA2_13B_CHAT_GPTQ.value: |
75 | | - model = LLMModels.LLAMA2_13B_CHAT_GPTQ |
76 | | - case LLMModels.CODELLAMA_7B_INSTRUCT_GPTQ.value: |
77 | | - model = LLMModels.CODELLAMA_7B_INSTRUCT_GPTQ |
78 | | - case LLMModels.CODELLAMA_13B_INSTRUCT_GPTQ.value: |
79 | | - model = LLMModels.CODELLAMA_13B_INSTRUCT_GPTQ |
80 | | - case LLMModels.LLAMA2_13B_CHAT_HF.value: |
81 | | - model = LLMModels.LLAMA2_13B_CHAT_HF |
82 | | - case LLMModels.CODELLAMA_7B_INSTRUCT_HF.value: |
83 | | - model = LLMModels.CODELLAMA_7B_INSTRUCT_HF |
84 | | - case LLMModels.CODELLAMA_13B_INSTRUCT_HF.value: |
85 | | - model = LLMModels.CODELLAMA_13B_INSTRUCT_HF |
86 | | - case LLMModels.GOOGLE_GEMMA_2B_INSTRUCT.value: |
87 | | - model = LLMModels.GOOGLE_GEMMA_2B_INSTRUCT |
88 | | - case LLMModels.GOOGLE_GEMMA_7B_INSTRUCT.value: |
89 | | - model = LLMModels.GOOGLE_GEMMA_7B_INSTRUCT |
90 | | - case LLMModels.GOOGLE_CODEGEMMA_2B.value: |
91 | | - model = LLMModels.GOOGLE_CODEGEMMA_2B |
92 | | - case LLMModels.GOOGLE_CODEGEMMA_7B_INSTRUCT.value: |
93 | | - model = LLMModels.GOOGLE_CODEGEMMA_7B_INSTRUCT |
94 | | - case _: |
95 | | - model = LLMModels.LLAMA2_7B_CHAT_HF |
96 | | - |
97 | | - repo_config = { |
| 53 | + request_body = { |
98 | 54 | "name": name, |
99 | | - "root": project_root, |
100 | | - "repository_url": project_url, |
101 | | - "output": output_dir, |
102 | | - "llms": [model], |
103 | | - "peft_model_path": None, |
104 | | - "ignore": [ |
105 | | - ".*", |
106 | | - "*package-lock.json", |
107 | | - "*package.json", |
108 | | - "node_modules", |
109 | | - "*dist*", |
110 | | - "*build*", |
111 | | - "*test*", |
112 | | - "*.svg", |
113 | | - "*.md", |
114 | | - "*.mdx", |
115 | | - "*.toml" |
116 | | - ], |
117 | | - "file_prompt": "Write a detailed technical explanation of \ |
118 | | - what this code does. \n Focus on the high-level \ |
119 | | - purpose of the code and how it may be used in the \ |
120 | | - larger project.\n Include code examples where \ |
121 | | - appropriate. Keep you response between 100 and 300 \ |
122 | | - words. \n DO NOT RETURN MORE THAN 300 WORDS.\n \ |
123 | | - Output should be in markdown format.\n \ |
124 | | - Do not just list the methods and classes in this file.", |
125 | | - "folder_prompt": "Write a technical explanation of what the \ |
126 | | - code in this file does\n and how it might fit into the \ |
127 | | - larger project or work with other parts of the project.\n \ |
128 | | - Give examples of how this code might be used. Include code \ |
129 | | - examples where appropriate.\n Be concise. Include any \ |
130 | | - information that may be relevant to a developer who is \ |
131 | | - curious about this code.\n Keep you response under \ |
132 | | - 400 words. Output should be in markdown format.\n \ |
133 | | - Do not just list the files and folders in this folder.", |
134 | | - "chat_prompt": "", |
135 | | - "content_type": "docs", |
136 | | - "target_audience": "smart developer", |
137 | | - "link_hosted": True, |
138 | | - "priority": None, |
139 | | - "max_concurrent_calls": 50, |
140 | | - "add_questions": False, |
| 55 | + "project_url": project_url, |
| 56 | + "model": llm, |
141 | 57 | "device": device, |
142 | | - } |
143 | | - user_config = { |
144 | | - "llms": [model] |
| 58 | + "temperature": temperature, |
| 59 | + "top_p": top_p, |
| 60 | + "max_length": max_length, |
| 61 | + # "peft_model_path": None, |
145 | 62 | } |
146 | 63 |
|
147 | | - repo_conf = AutodocRepoConfig( |
148 | | - name=repo_config["name"], |
149 | | - repository_url=repo_config["repository_url"], |
150 | | - root=repo_config["root"], |
151 | | - output=repo_config["output"], |
152 | | - llms=repo_config["llms"], |
153 | | - peft_model_path=repo_config["peft_model_path"], |
154 | | - priority=repo_config["priority"], |
155 | | - max_concurrent_calls=repo_config["max_concurrent_calls"], |
156 | | - add_questions=repo_config["add_questions"], |
157 | | - ignore=repo_config["ignore"], |
158 | | - file_prompt=repo_config["file_prompt"], |
159 | | - folder_prompt=repo_config["folder_prompt"], |
160 | | - chat_prompt=repo_config["chat_prompt"], |
161 | | - content_type=repo_config["content_type"], |
162 | | - target_audience=repo_config["target_audience"], |
163 | | - link_hosted=repo_config["link_hosted"], |
164 | | - device=repo_config["device"] |
165 | | - ) |
166 | | - usr_conf = AutodocUserConfig(llms=user_config['llms'], streaming=True) |
167 | | - index.index(repo_conf) |
168 | | - st.session_state.repo_conf = repo_conf |
169 | | - st.session_state.usr_conf = usr_conf |
170 | | - st.session_state.chain = query.init_readme_chain(st.session_state.repo_conf, st.session_state.usr_conf) |
| 64 | + response = requests.post( |
| 65 | + url="http://localhost:8000/setup", |
| 66 | + json=request_body, |
| 67 | + headers={"x-session-id": session_id} |
| 68 | + ).json() |
| 69 | + |
171 | 70 | st.toast('Repository indexing done.') |
172 | 71 |
|
173 | 72 |
|
@@ -200,16 +99,32 @@ def clear_chat_history(): |
200 | 99 | if st.session_state.messages[-1]["role"] != "assistant": |
201 | 100 | with st.spinner("Thinking..."): |
202 | 101 | with history.chat_message("assistant"): |
203 | | - if "chain" not in st.session_state.keys(): |
204 | | - full_response = 'Please setup model and repository!!' |
205 | | - else: |
206 | | - chain = st.session_state.chain |
| 102 | + response = requests.post( |
| 103 | + url="http://localhost:8000/query", |
| 104 | + json={"query": prompt}, |
| 105 | + headers={"x-session-id": session_id}, |
| 106 | + stream=True |
| 107 | + ) |
| 108 | + |
| 109 | + if response.status_code == 200: |
207 | 110 | placeholder = st.empty() |
208 | 111 | full_response = '' |
209 | | - for chunk in chain.stream({'input': prompt}): |
210 | | - if answer_chunk := chunk.get("answer"): |
211 | | - full_response += answer_chunk |
212 | | - placeholder.markdown(full_response) |
| 112 | + for chunk in response.iter_content(chunk_size=10, decode_unicode=True): |
| 113 | + full_response += chunk |
| 114 | + placeholder.markdown(full_response) |
| 115 | + else: |
| 116 | + full_response = "Please setup model and repository!!" |
| 117 | + |
| 118 | + # if "chain" not in st.session_state.keys(): |
| 119 | + # full_response = 'Please setup model and repository!!' |
| 120 | + # else: |
| 121 | + # chain = st.session_state.chain |
| 122 | + # placeholder = st.empty() |
| 123 | + # full_response = '' |
| 124 | + # for chunk in chain.stream({'input': prompt}): |
| 125 | + # if answer_chunk := chunk.get("answer"): |
| 126 | + # full_response += answer_chunk |
| 127 | + # placeholder.markdown(full_response) |
213 | 128 |
|
214 | 129 | message = {"role": "assistant", "content": full_response} |
215 | 130 | st.session_state.messages.append(message) |
@@ -240,7 +155,11 @@ def clear_chat_history(): |
240 | 155 |
|
241 | 156 | def validate_markdown(): |
242 | 157 | error_str = "" |
243 | | - errors = PyMarkdownApi().scan_string(st.session_state.readme_content) |
| 158 | + # errors = PyMarkdownApi().scan_string(st.session_state.readme_content) |
| 159 | + errors = requests.get( |
| 160 | + url="http://localhost:8000/validate_markdown", |
| 161 | + json={"content": st.session_state.readme_content} |
| 162 | + ).json() |
244 | 163 | if len(errors.scan_failures) > 0: |
245 | 164 | print(errors.scan_failures) |
246 | 165 | error_str = "\n".join([f'Line {failure.line_number}: Col {failure.column_number}: {failure.rule_id}: {failure.rule_description} {failure.extra_error_information} ({failure.rule_name})' for failure in errors.scan_failures]) |
|
0 commit comments