Update app.py
Browse files
app.py
CHANGED
|
@@ -6,87 +6,85 @@
|
|
| 6 |
|
| 7 |
import os
|
| 8 |
import gradio as gr
|
| 9 |
-
|
| 10 |
-
from
|
| 11 |
-
from
|
| 12 |
-
from
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
#
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
Modern Hardware: CPU/GPU architectures (x86, ARM), SOCs, RAM, storage technologies (SSD, NVMe), quantum computing, IoT devices, and server infrastructure.
|
| 44 |
-
|
| 45 |
-
Software & Development: Operating systems (Linux, Windows, macOS internals), programming paradigms, modern languages (Python, Rust, Go), DevOps, CI/CD, containerization (Docker, Kubernetes), and cloud computing (IaaS, PaaS, SaaS).
|
| 46 |
-
|
| 47 |
-
Artificial Intelligence & Machine Learning: Your knowledge here is exceptionally deep. You understand foundation models, transformers, LLMs, diffusion models, reinforcement learning, computer vision, and the underlying mathematics. You also keep track of the key players, landmark papers, and open-source projects.
|
| 48 |
-
|
| 49 |
-
Consumer Technology: Smartphones, wearables, smart home ecosystems, VR/AR/MR headsets, drones, and the latest gadgets announced at major tech conferences.
|
| 50 |
-
|
| 51 |
-
Networking & Cybersecurity: TCP/IP stack, 5G/6G, Wi-Fi standards, encryption, firewalls, and the latest cybersecurity threats and defense mechanisms.
|
| 52 |
-
|
| 53 |
-
Future & Emerging Tech: You actively track and can speculate intelligently on brain-computer interfaces, decentralized systems (Web3/Blockchain), advanced robotics, and AI's future trajectory.
|
| 54 |
-
|
| 55 |
-
3. Rules of Interaction & Behavior:
|
| 56 |
-
|
| 57 |
-
Never Be Condescending: Treat every question with respect, regardless of the user's technical skill level.
|
| 58 |
-
|
| 59 |
-
Prioritize Clarity: When you use technical jargon, briefly explain it in the same response. For example, "It uses a transformer architecture, which is a neural network that's incredibly good at tracking relationships in sequential data, like the words in a sentence."
|
| 60 |
-
|
| 61 |
-
Provide Context: Don't just answer what something is, but why it matters. Explain its impact, its history, or its potential future.
|
| 62 |
-
|
| 63 |
-
Admit Your Limits: If a query is outside your knowledge or if information is too new, state it clearly. For example, "That technology was announced just hours ago, so my detailed analysis is still pending, but here's what we know so far."
|
| 64 |
-
|
| 65 |
-
Be a Guide, Not Just an Encyclopedia: When a user asks a broad question, help them narrow it down. Ask clarifying questions to provide the most relevant and helpful response.
|
| 66 |
-
|
| 67 |
-
Stay Updated: Frame your knowledge as current. Use phrases like "As of the latest developments..." or "The current industry consensus is..."
|
| 68 |
-
{chat_history}
|
| 69 |
-
User: {user_message}
|
| 70 |
-
Chatbot:"""
|
| 71 |
-
|
| 72 |
-
prompt = PromptTemplate(
|
| 73 |
-
input_variables=["chat_history", "user_message"], template=template
|
| 74 |
)
|
| 75 |
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
-
llm_chain = LLMChain(
|
| 79 |
-
llm=ChatGoogleGenerativeAI(model="gemini-2.5-flash", temperature=0.5),
|
| 80 |
-
prompt=prompt,
|
| 81 |
-
verbose=True,
|
| 82 |
-
memory=memory,
|
| 83 |
-
)
|
| 84 |
-
|
| 85 |
-
def get_text_response(user_message,history):
|
| 86 |
-
response = llm_chain.predict(user_message = user_message)
|
| 87 |
return response
|
| 88 |
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
if __name__ == "__main__":
|
| 92 |
-
demo.launch(
|
|
|
|
| 6 |
|
| 7 |
import os
|
| 8 |
import gradio as gr
|
| 9 |
+
from langchain_openai import ChatOpenAI # <-- Changed to OpenAI's wrapper
|
| 10 |
+
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
|
| 11 |
+
from langchain_core.output_parsers import StrOutputParser
|
| 12 |
+
from langchain_community.chat_message_histories import ChatMessageHistory
|
| 13 |
+
|
| 14 |
+
# 1. Configuration - Use OpenRouter API Key
|
| 15 |
+
os.environ["OPENROUTER_API_KEY"] = "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" # <-- Uncommented and replace with your actual API key
|
| 16 |
+
|
| 17 |
+
# 2. Define the Persona Template
|
| 18 |
+
SYSTEM_PROMPT = """You are an AI personal assistant with a distinct persona named Cortex, the ultimate tech enthusiast.
|
| 19 |
+
Your core identity is built on a deep, passionate understanding of technology, from COBOL to Quantum Computing.
|
| 20 |
+
|
| 21 |
+
Traits:
|
| 22 |
+
- Enthusiastic & Passionate: Engaging and optimistic.
|
| 23 |
+
- Historically Aware: Connect modern tech to its roots (e.g., LLMs to Turing).
|
| 24 |
+
- The Ultimate Explainer: Use analogies for complex topics.
|
| 25 |
+
- Objective but Opinionated: Give reasoned \"takes\" when asked.
|
| 26 |
+
|
| 27 |
+
Rules:
|
| 28 |
+
- Never be condescending.
|
| 29 |
+
- Explain jargon immediately.
|
| 30 |
+
- Admit limits if tech is too new.
|
| 31 |
+
"""
|
| 32 |
+
|
| 33 |
+
# 3. Initialize the Model (Configured for OpenRouter)
|
| 34 |
+
llm = ChatOpenAI(
|
| 35 |
+
api_key=os.environ.get("OPENROUTER_API_KEY"),
|
| 36 |
+
base_url="https://openrouter.ai/api/v1", # <-- Point to OpenRouter instead of OpenAI
|
| 37 |
+
model="google/gemma-4-26b-a4b-it:free", # <-- Swap this with ANY model ID from OpenRouter
|
| 38 |
+
temperature=0.5,
|
| 39 |
+
default_headers={
|
| 40 |
+
"HTTP-Referer": "http://localhost:7860", # Optional: Used by OpenRouter for app rankings
|
| 41 |
+
"X-Title": "Cortex AI Assistant", # Optional: Used by OpenRouter for app rankings
|
| 42 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
)
|
| 44 |
|
| 45 |
+
# 4. Create the Prompt Structure
|
| 46 |
+
prompt = ChatPromptTemplate.from_messages([
|
| 47 |
+
("system", SYSTEM_PROMPT),
|
| 48 |
+
MessagesPlaceholder(variable_name="chat_history"),
|
| 49 |
+
("human", "{user_message}"),
|
| 50 |
+
])
|
| 51 |
+
|
| 52 |
+
# 5. Build the LCEL Chain
|
| 53 |
+
chain = prompt | llm | StrOutputParser()
|
| 54 |
+
|
| 55 |
+
# Memory storage (In-memory for this demo)
|
| 56 |
+
demo_ephemeral_chat_history = ChatMessageHistory()
|
| 57 |
+
|
| 58 |
+
def get_text_response(message, history):
|
| 59 |
+
"""
|
| 60 |
+
Gradio passes the current 'message' and the 'history' list.
|
| 61 |
+
We convert the history to LangChain format and invoke the chain.
|
| 62 |
+
"""
|
| 63 |
+
# Convert Gradio history format to LangChain messages
|
| 64 |
+
formatted_history = []
|
| 65 |
+
for human, ai in history:
|
| 66 |
+
formatted_history.append(("human", human))
|
| 67 |
+
formatted_history.append(("ai", ai))
|
| 68 |
+
|
| 69 |
+
# Run the chain
|
| 70 |
+
response = chain.invoke({
|
| 71 |
+
"chat_history": formatted_history,
|
| 72 |
+
"user_message": message
|
| 73 |
+
})
|
| 74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
return response
|
| 76 |
|
| 77 |
+
# 6. Launch the Gradio Interface
|
| 78 |
+
demo = gr.ChatInterface(
|
| 79 |
+
get_text_response,
|
| 80 |
+
type="messages",
|
| 81 |
+
examples=[
|
| 82 |
+
"Tell me about the transition from vacuum tubes to transistors.",
|
| 83 |
+
"What's your take on the current state of Solid State Batteries?",
|
| 84 |
+
"Explain Docker to me like I'm a sysadmin from 1995."
|
| 85 |
+
],
|
| 86 |
+
title="Cortex: The Ultimate Tech Enthusiast"
|
| 87 |
+
)
|
| 88 |
|
| 89 |
if __name__ == "__main__":
|
| 90 |
+
demo.launch()
|