anirudh248 commited on
Commit
1ce2187
·
verified ·
1 Parent(s): 8ce8923

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -40
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. Load Vector Store
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. Load Model & Pipeline in 4-bit
20
- model_id = "anirudh248/llama3-upf-generator"
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. Setup RAG Chain & Chat Prompt
49
- upf_prompt_template = """
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
- Power Intent Description:
56
- {input}
57
 
58
- Generate only the UPF code. Do not include introductory text.
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
- chat_prompt = PromptTemplate.from_template(
65
- "You are a helpful AI assistant. Answer the user's message conversationally and concisely.\n\nUser: {input}\nAssistant:"
66
- )
67
 
68
- def generate_upf_code(power_intent_description):
69
- result = rag_chain.invoke({"input": power_intent_description})
70
- return result['answer'].strip()
71
 
72
- def generate_chat_response(user_message):
73
- return llm.invoke(chat_prompt.format(input=user_message)).strip()
 
74
 
75
- # 4. Routing Logic & Gradio Interface
76
- UPF_KEYWORDS = [
77
- "upf", "power", "domain", "isolation", "retention", "voltage",
78
- "switch", "supply", "pst", "level shifter", "state", "intent",
79
- "create_", "set_", "connect_"
80
- ]
81
 
82
  def user_interaction(user_message, history):
83
  history = history or []
84
- msg_lower = user_message.lower()
85
 
86
- is_upf_request = any(kw in msg_lower for kw in UPF_KEYWORDS) or len(msg_lower.split()) > 12
87
-
88
- if is_upf_request:
89
- response = generate_upf_code(user_message)
90
- if "```" not in response:
91
- response = f"```tcl\n{response}\n```"
92
- else:
93
- response = generate_chat_response(user_message)
 
 
94
 
95
  history.append({"role": "user", "content": user_message})
96
- history.append({"role": "assistant", "content": response})
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