Update app.py
Browse files
app.py
CHANGED
|
@@ -7,17 +7,17 @@ from langchain_classic.chains import create_retrieval_chain
|
|
| 7 |
from langchain_classic.chains.combine_documents import create_stuff_documents_chain
|
| 8 |
from langchain_core.prompts import PromptTemplate
|
| 9 |
|
| 10 |
-
# 1.
|
| 11 |
embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2')
|
| 12 |
vectorstore = FAISS.load_local(
|
| 13 |
"faiss_upf_index",
|
| 14 |
embeddings,
|
| 15 |
allow_dangerous_deserialization=True
|
| 16 |
)
|
| 17 |
-
retriever = vectorstore.as_retriever()
|
| 18 |
|
| 19 |
-
# 2.
|
| 20 |
-
model_id = "anirudh248/
|
| 21 |
|
| 22 |
bnb_config = BitsAndBytesConfig(
|
| 23 |
load_in_4bit=True,
|
|
@@ -45,59 +45,53 @@ hf_pipeline = pipeline(
|
|
| 45 |
)
|
| 46 |
llm = HuggingFacePipeline(pipeline=hf_pipeline)
|
| 47 |
|
| 48 |
-
# 3.
|
| 49 |
-
|
| 50 |
-
You are an expert in Unified Power Format (UPF). Generate a precise and complete UPF code block based on the following power intent. Use the retrieved context as a reference. The code must be correct and adhere to UPF 3.0 standards.
|
| 51 |
|
| 52 |
-
Context:
|
| 53 |
{context}
|
| 54 |
|
| 55 |
-
|
| 56 |
-
{
|
| 57 |
|
| 58 |
-
|
| 59 |
-
"""
|
| 60 |
-
upf_prompt = PromptTemplate.from_template(upf_prompt_template)
|
| 61 |
-
document_chain = create_stuff_documents_chain(llm, upf_prompt)
|
| 62 |
-
rag_chain = create_retrieval_chain(retriever, document_chain)
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
|
| 68 |
-
|
| 69 |
-
result = rag_chain.invoke({"input": power_intent_description})
|
| 70 |
-
return result['answer'].strip()
|
| 71 |
|
| 72 |
-
|
| 73 |
-
|
|
|
|
| 74 |
|
| 75 |
-
# 4.
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
]
|
| 81 |
|
| 82 |
def user_interaction(user_message, history):
|
| 83 |
history = history or []
|
| 84 |
-
msg_lower = user_message.lower()
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
|
|
|
|
|
|
| 94 |
|
| 95 |
history.append({"role": "user", "content": user_message})
|
| 96 |
-
history.append({"role": "assistant", "content":
|
| 97 |
return history, ""
|
| 98 |
|
| 99 |
with gr.Blocks() as interface:
|
| 100 |
-
gr.Markdown("# UPF Code Generator with Llama 3 & RAG")
|
| 101 |
|
| 102 |
chatbot = gr.Chatbot(label="Chat History", elem_id="chatbot")
|
| 103 |
|
|
|
|
| 7 |
from langchain_classic.chains.combine_documents import create_stuff_documents_chain
|
| 8 |
from langchain_core.prompts import PromptTemplate
|
| 9 |
|
| 10 |
+
# 1. Vector Store Setup
|
| 11 |
embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2')
|
| 12 |
vectorstore = FAISS.load_local(
|
| 13 |
"faiss_upf_index",
|
| 14 |
embeddings,
|
| 15 |
allow_dangerous_deserialization=True
|
| 16 |
)
|
| 17 |
+
retriever = vectorstore.as_retriever(search_kwargs={"k": 3})
|
| 18 |
|
| 19 |
+
# 2. Model & Pipeline Initialization
|
| 20 |
+
model_id = "anirudh248/upf-code-generator"
|
| 21 |
|
| 22 |
bnb_config = BitsAndBytesConfig(
|
| 23 |
load_in_4bit=True,
|
|
|
|
| 45 |
)
|
| 46 |
llm = HuggingFacePipeline(pipeline=hf_pipeline)
|
| 47 |
|
| 48 |
+
# 3. Unified RAG Chain
|
| 49 |
+
unified_prompt_template = """You are a highly capable AI assistant specializing in Unified Power Format (UPF 3.0) and VLSI power intent design.
|
|
|
|
| 50 |
|
| 51 |
+
Context (Reference UPF examples, if relevant):
|
| 52 |
{context}
|
| 53 |
|
| 54 |
+
Conversation History:
|
| 55 |
+
{chat_history}
|
| 56 |
|
| 57 |
+
User Input: {input}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
+
Instructions:
|
| 60 |
+
1. Analyze the User Input. If it requests UPF code, power intent design, or hardware specifics, act as an expert UPF engineer. Use the Context to generate precise UPF 3.0 code, enclosed completely in ```tcl ... ``` blocks.
|
| 61 |
+
2. If the User Input is a general question, greeting, or unrelated to UPF, respond normally as a helpful AI assistant. Ignore the UPF Context and do not generate code.
|
| 62 |
|
| 63 |
+
Assistant:"""
|
|
|
|
|
|
|
| 64 |
|
| 65 |
+
unified_prompt = PromptTemplate.from_template(unified_prompt_template)
|
| 66 |
+
document_chain = create_stuff_documents_chain(llm, unified_prompt)
|
| 67 |
+
rag_chain = create_retrieval_chain(retriever, document_chain)
|
| 68 |
|
| 69 |
+
# 4. Gradio Interface & Handling
|
| 70 |
+
def format_history(history):
|
| 71 |
+
if not history:
|
| 72 |
+
return "No previous conversation."
|
| 73 |
+
return "\n".join([f"{msg['role'].capitalize()}: {msg['content']}" for msg in history])
|
|
|
|
| 74 |
|
| 75 |
def user_interaction(user_message, history):
|
| 76 |
history = history or []
|
|
|
|
| 77 |
|
| 78 |
+
response = rag_chain.invoke({
|
| 79 |
+
"input": user_message,
|
| 80 |
+
"chat_history": format_history(history)
|
| 81 |
+
})
|
| 82 |
+
|
| 83 |
+
answer = response['answer'].strip()
|
| 84 |
+
|
| 85 |
+
upf_syntax_hints = ["create_power_domain", "set_isolation", "set_retention", "create_supply_port", "create_power_switch"]
|
| 86 |
+
if "```" not in answer and any(kw in answer.lower() for kw in upf_syntax_hints):
|
| 87 |
+
answer = f"```tcl\n{answer}\n```"
|
| 88 |
|
| 89 |
history.append({"role": "user", "content": user_message})
|
| 90 |
+
history.append({"role": "assistant", "content": answer})
|
| 91 |
return history, ""
|
| 92 |
|
| 93 |
with gr.Blocks() as interface:
|
| 94 |
+
gr.Markdown("# ⚡ UPF Code Generator with Llama 3 & RAG")
|
| 95 |
|
| 96 |
chatbot = gr.Chatbot(label="Chat History", elem_id="chatbot")
|
| 97 |
|