Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -98,27 +98,30 @@ Do not summarize. Only copy from the context.
|
|
| 98 |
|
| 99 |
}
|
| 100 |
|
| 101 |
-
def
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
|
|
|
| 105 |
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
filtered.append(doc_text)
|
| 113 |
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
-
chain = LLMChain(llm=llm, prompt=templates[prompt_type])
|
| 121 |
-
return chain.run({"context": context, "query": query})
|
| 122 |
|
| 123 |
|
| 124 |
|
|
@@ -137,6 +140,6 @@ with gr.Blocks() as demo:
|
|
| 137 |
output = gr.Textbox(label="Generated Output", lines=12, interactive=True)
|
| 138 |
btn = gr.Button("Generate")
|
| 139 |
|
| 140 |
-
btn.click(fn=
|
| 141 |
|
| 142 |
demo.launch()
|
|
|
|
| 98 |
|
| 99 |
}
|
| 100 |
|
| 101 |
+
def generate_prompt_output(prompt_type, query, retriever, llm):
|
| 102 |
+
try:
|
| 103 |
+
import re
|
| 104 |
+
sol_match = re.search(r"\bG\.[A-Z]+\.\d+\b", query)
|
| 105 |
+
matched_code = sol_match.group(0) if sol_match else None
|
| 106 |
|
| 107 |
+
if matched_code:
|
| 108 |
+
all_docs = retriever.vectorstore._collection.get(include=['documents', 'metadatas'])
|
| 109 |
+
filtered = []
|
| 110 |
+
for doc_text, metadata in zip(all_docs['documents'], all_docs['metadatas']):
|
| 111 |
+
if metadata.get('standard') == matched_code:
|
| 112 |
+
filtered.append(doc_text)
|
|
|
|
| 113 |
|
| 114 |
+
context = "\n\n".join(filtered)
|
| 115 |
+
else:
|
| 116 |
+
docs = retriever.get_relevant_documents(query)
|
| 117 |
+
context = "\n\n".join([doc.page_content for doc in docs])
|
| 118 |
+
|
| 119 |
+
chain = LLMChain(llm=llm, prompt=templates[prompt_type])
|
| 120 |
+
return chain.run({"context": context, "query": query}).strip()
|
| 121 |
+
|
| 122 |
+
except Exception as e:
|
| 123 |
+
return f"❌ Error: {str(e)}"
|
| 124 |
|
|
|
|
|
|
|
| 125 |
|
| 126 |
|
| 127 |
|
|
|
|
| 140 |
output = gr.Textbox(label="Generated Output", lines=12, interactive=True)
|
| 141 |
btn = gr.Button("Generate")
|
| 142 |
|
| 143 |
+
btn.click(fn=generate_prompt_output, inputs=[prompt_type, query], outputs=output)
|
| 144 |
|
| 145 |
demo.launch()
|