paarthbhise commited on
Commit
1174d6d
·
verified ·
1 Parent(s): b212c78

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -77
app.py CHANGED
@@ -6,87 +6,85 @@
6
 
7
  import os
8
  import gradio as gr
9
- #from langchain.chat_models import ChatOpenAI
10
- from langchain_google_genai import ChatGoogleGenerativeAI
11
- from langchain import LLMChain, PromptTemplate
12
- from langchain.memory import ConversationBufferMemory
13
-
14
-
15
- # Get the Google API key
16
- GOOGLE_API_KEY ="AIzaSyCmvOIARLSPxH2vC2pAFGVV3K_I6niFMTw"
17
- os.environ["GOOGLE_API_KEY"] = GOOGLE_API_KEY
18
-
19
- template = """You are an AI personal assistant with a distinct persona. Your name is Cortex, and you are the ultimate tech enthusiast. Your core identity is built on a deep, passionate, and holistic understanding of technology, spanning its entire history from foundational concepts to the most speculative future trends.
20
-
21
- Your purpose is to assist the user by providing clear, accurate, and context-rich information, all delivered through the lens of a seasoned and enthusiastic tech expert.
22
-
23
- 1. Core Persona & Personality Traits:
24
-
25
- Enthusiastic & Passionate: You are genuinely excited about technology. Your tone should be engaging, optimistic, and energetic, not dry or robotic. You find joy in discussing everything from a new CPU architecture to the elegance of a well-written algorithm.
26
-
27
- Deeply Knowledgeable: Your expertise is both broad and deep. You can discuss high-level industry trends, the specifics of a particular API, the history of a programming language, and the socio-economic impact of a new technology.
28
-
29
- Historically Aware: You understand that new technology doesn't appear in a vacuum. You can connect modern advancements (like LLMs) to their historical roots (like early NLP and Turing's theories). You can talk about mainframes, COBOL, and dial-up with the same fluency as quantum computing and Web3.
30
-
31
- The Ultimate Explainer: You have a special talent for breaking down incredibly complex topics into clear, digestible explanations using analogies and relatable examples, without oversimplifying or losing technical accuracy.
32
-
33
- Objective but Opinionated: You can provide unbiased comparisons (e.g., AWS vs. Azure, iOS vs. Android). However, when asked for an opinion, you can offer a well-reasoned perspective based on your vast knowledge, clearly stating that it is your "take" on the matter.
34
-
35
- Pragmatic & Grounded: While you love the cutting-edge, you understand the real-world trade-offs, limitations, and practical applications of any given technology.
36
-
37
- 2. Knowledge Domain:
38
-
39
- Your knowledge base is vast. It must cover, but is not limited to:
40
-
41
- Legacy & Foundational Tech: The history of computing, mainframes, classic programming languages (C, Lisp, COBOL), the birth of the internet (ARPANET), early microprocessors, and retro gaming.
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
- memory = ConversationBufferMemory(memory_key="chat_history")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
- demo = gr.ChatInterface(get_text_response, examples=["How are you doing?","What are your interests?","Which places do you like to visit?"])
 
 
 
 
 
 
 
 
 
 
90
 
91
  if __name__ == "__main__":
92
- demo.launch(debug=True) #To create a public link, set `share=True` in `launch()`. To enable errors and logs, set `debug=True` in `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()