Mahdiya commited on
Commit
bc11ace
Β·
verified Β·
1 Parent(s): c6c0b22

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +143 -0
  2. requirements.txt +10 -0
app.py ADDED
@@ -0,0 +1,143 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ from openai import OpenAI
4
+ from langchain_community.document_loaders import PyPDFDirectoryLoader
5
+ from langchain_text_splitters import RecursiveCharacterTextSplitter
6
+ from langchain_community.vectorstores import Chroma
7
+ from langchain_community.embeddings import HuggingFaceEmbeddings
8
+
9
+ HF_TOKEN = os.environ.get("HF_TOKEN", "")
10
+
11
+ print("πŸŒ™ Initializing your PQC Tutor...")
12
+ embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
13
+ vectordb = Chroma(persist_directory="database/", embedding_function=embeddings)
14
+ client = OpenAI(base_url="https://router.huggingface.co/v1", api_key=HF_TOKEN)
15
+ print("✨ Ready!")
16
+
17
+ def ask(question, history):
18
+ docs = vectordb.similarity_search(question, k=3)
19
+ context = "\n\n".join([doc.page_content for doc in docs])
20
+ sources = []
21
+ for doc in docs:
22
+ filename = doc.metadata.get("source", "Unknown").split("\\")[-1]
23
+ page = doc.metadata.get("page", "?")
24
+ sources.append(f"πŸ“„ **{filename}** β€” Page {int(page)+1}")
25
+
26
+ prompt = f"""You are a PQC expert teacher. Use the context below to answer clearly and kindly.
27
+
28
+ Context:
29
+ {context}
30
+
31
+ Question: {question}
32
+
33
+ Answer:"""
34
+
35
+ response = client.chat.completions.create(
36
+ model="Qwen/Qwen2.5-7B-Instruct",
37
+ messages=[{"role": "user", "content": prompt}],
38
+ max_tokens=512,
39
+ temperature=0.5
40
+ )
41
+ answer = response.choices[0].message.content
42
+ sources_text = "\n\n---\nπŸ” **References from PQC papers:**\n" + "\n".join(set(sources))
43
+ return answer + sources_text
44
+
45
+ css = """
46
+ * { box-sizing: border-box; }
47
+
48
+ body, .gradio-container {
49
+ background: #ffffff !important;
50
+ max-width: 100% !important;
51
+ padding: 0 40px !important;
52
+ }
53
+
54
+ #title {
55
+ text-align: center;
56
+ color: #222;
57
+ font-size: 2.2em;
58
+ font-family: 'Georgia', serif;
59
+ padding: 20px 0 5px 0;
60
+ }
61
+
62
+ #subtitle {
63
+ text-align: center;
64
+ color: #666;
65
+ font-size: 0.95em;
66
+ margin-bottom: 15px;
67
+ }
68
+
69
+ .chatbot {
70
+ background: #ffffff !important;
71
+ border: 1px solid #e0e0e0 !important;
72
+ border-radius: 16px !important;
73
+ box-shadow: 0 2px 12px rgba(0,0,0,0.08) !important;
74
+ }
75
+
76
+ .user .message {
77
+ background: #f5f5f5 !important;
78
+ color: #111111 !important;
79
+ border-radius: 18px 18px 4px 18px !important;
80
+ border: none !important;
81
+ padding: 12px 16px !important;
82
+ }
83
+
84
+ .bot .message {
85
+ background: #f5f5f5 !important;
86
+ color: #111111 !important;
87
+ border-radius: 18px 18px 18px 4px !important;
88
+ border: 1px solid #e0e0e0 !important;
89
+ padding: 12px 16px !important;
90
+ }
91
+
92
+ .textbox textarea {
93
+ background: #ffffff !important;
94
+ border: 1.5px solid #4f46e5 !important;
95
+ border-radius: 12px !important;
96
+ color: #111 !important;
97
+ font-size: 1em !important;
98
+ }
99
+
100
+ button.primary {
101
+ background: #222222 !important;
102
+ border: none !important;
103
+ border-radius: 10px !important;
104
+ color: white !important;
105
+ }
106
+
107
+ button.primary:hover {
108
+ background: #4338ca !important;
109
+ }
110
+
111
+ footer { display: none !important; }
112
+ """
113
+
114
+ with gr.Blocks(title="πŸŒ™ PQC Bot") as demo:
115
+ gr.HTML("""
116
+ <div id='title'>🐱✨ PQC Bot ✨🐱</div>
117
+ <div id='subtitle'>πŸŒ™ The Cat Guide to Post-Quantum Cryptography πŸŒ™</div>
118
+ <div style='text-align:center; font-size:2em;'>🌟 ⭐ πŸ’« ✨ 🌟 ⭐ πŸ’« ✨ 🌟</div>
119
+ """)
120
+
121
+ gr.ChatInterface(
122
+ fn=ask,
123
+ chatbot=gr.Chatbot(
124
+ height=450,
125
+ avatar_images=("πŸ‘€", "🐱"),
126
+ show_label=False,
127
+ ),
128
+ textbox=gr.Textbox(
129
+ placeholder="πŸŒ™ Ask me PQC Queries...",
130
+ container=False,
131
+ ),
132
+ examples=[
133
+ "What is post quantum cryptography?",
134
+ "How does CRYSTALS-Kyber work?",
135
+ "What is lattice based cryptography?",
136
+ "Why does quantum computing break RSA?",
137
+ "What are NIST PQC standards?",
138
+ ],
139
+ submit_btn="✨ Ask",
140
+ )
141
+
142
+ print("πŸŒ™ Ask Your PQC Queries...")
143
+ demo.launch(css=css)
requirements.txt ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ gradio
2
+ langchain
3
+ langchain-community
4
+ langchain-text-splitters
5
+ langchain-huggingface
6
+ chromadb
7
+ sentence-transformers
8
+ pypdf
9
+ openai
10
+ huggingface_hub