Update app.py
Browse files
app.py
CHANGED
|
@@ -50,40 +50,66 @@ hf_pipeline = pipeline(
|
|
| 50 |
llm = HuggingFacePipeline(pipeline=hf_pipeline)
|
| 51 |
|
| 52 |
# ==========================================
|
| 53 |
-
# 3. Setup
|
| 54 |
# ==========================================
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
|
|
|
| 58 |
|
| 59 |
-
Context
|
| 60 |
{context}
|
| 61 |
|
| 62 |
-
|
| 63 |
{input}
|
| 64 |
|
| 65 |
-
|
| 66 |
-
1. If the User's Input is asking to generate UPF code or discuss power intent, act as an expert UPF engineer. Use the Context above to generate precise, complete UPF 3.0 code. Enclose the code cleanly in ```tcl ... ``` blocks.
|
| 67 |
-
2. If the User's Input is a general question, a greeting, or unrelated to UPF, respond normally as a helpful AI assistant and ignore the UPF context.
|
| 68 |
-
|
| 69 |
-
Response:
|
| 70 |
"""
|
| 71 |
-
|
| 72 |
-
document_chain = create_stuff_documents_chain(llm,
|
| 73 |
rag_chain = create_retrieval_chain(retriever, document_chain)
|
| 74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
def generate_upf_code(power_intent_description):
|
| 76 |
result = rag_chain.invoke({"input": power_intent_description})
|
| 77 |
return result['answer'].strip()
|
| 78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
# ==========================================
|
| 80 |
-
# 4. Gradio UI
|
| 81 |
# ==========================================
|
| 82 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
@spaces.GPU(duration=120)
|
| 84 |
def user_interaction(user_message, history):
|
| 85 |
history = history or []
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
history.append({"role": "user", "content": user_message})
|
| 89 |
history.append({"role": "assistant", "content": response})
|
|
|
|
| 50 |
llm = HuggingFacePipeline(pipeline=hf_pipeline)
|
| 51 |
|
| 52 |
# ==========================================
|
| 53 |
+
# 3. Setup Dual Chains
|
| 54 |
# ==========================================
|
| 55 |
|
| 56 |
+
# CHAIN A: The UPF Code Generator
|
| 57 |
+
upf_prompt_template = """
|
| 58 |
+
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.
|
| 59 |
|
| 60 |
+
Context:
|
| 61 |
{context}
|
| 62 |
|
| 63 |
+
Power Intent Description:
|
| 64 |
{input}
|
| 65 |
|
| 66 |
+
Generate only the UPF code. Do not include introductory text.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
"""
|
| 68 |
+
upf_prompt = PromptTemplate.from_template(upf_prompt_template)
|
| 69 |
+
document_chain = create_stuff_documents_chain(llm, upf_prompt)
|
| 70 |
rag_chain = create_retrieval_chain(retriever, document_chain)
|
| 71 |
|
| 72 |
+
# CHAIN B: The General Conversationalist
|
| 73 |
+
chat_prompt = PromptTemplate.from_template("""You are a helpful AI assistant. Answer the user's message conversationally and concisely.
|
| 74 |
+
|
| 75 |
+
User: {input}
|
| 76 |
+
Assistant:""")
|
| 77 |
+
|
| 78 |
def generate_upf_code(power_intent_description):
|
| 79 |
result = rag_chain.invoke({"input": power_intent_description})
|
| 80 |
return result['answer'].strip()
|
| 81 |
|
| 82 |
+
def generate_chat_response(user_message):
|
| 83 |
+
return llm.invoke(chat_prompt.format(input=user_message)).strip()
|
| 84 |
+
|
| 85 |
+
|
| 86 |
# ==========================================
|
| 87 |
+
# 4. Gradio UI & Routing Logic
|
| 88 |
# ==========================================
|
| 89 |
|
| 90 |
+
# Keywords that trigger the high-quality UPF generation path
|
| 91 |
+
UPF_KEYWORDS = [
|
| 92 |
+
"upf", "power", "domain", "isolation", "retention", "voltage",
|
| 93 |
+
"switch", "supply", "pst", "level shifter", "state", "intent",
|
| 94 |
+
"create_", "set_", "connect_"
|
| 95 |
+
]
|
| 96 |
+
|
| 97 |
@spaces.GPU(duration=120)
|
| 98 |
def user_interaction(user_message, history):
|
| 99 |
history = history or []
|
| 100 |
+
|
| 101 |
+
msg_lower = user_message.lower()
|
| 102 |
+
|
| 103 |
+
is_upf_request = any(kw in msg_lower for kw in UPF_KEYWORDS) or len(msg_lower.split()) > 12
|
| 104 |
+
|
| 105 |
+
if is_upf_request:
|
| 106 |
+
# PATH A: High-quality UPF Generation
|
| 107 |
+
response = generate_upf_code(user_message)
|
| 108 |
+
if "```" not in response:
|
| 109 |
+
response = f"```tcl\n{response}\n```"
|
| 110 |
+
else:
|
| 111 |
+
# PATH B: Normal Conversation
|
| 112 |
+
response = generate_chat_response(user_message)
|
| 113 |
|
| 114 |
history.append({"role": "user", "content": user_message})
|
| 115 |
history.append({"role": "assistant", "content": response})
|