eabybabu commited on
Commit
a77ed8b
Β·
verified Β·
1 Parent(s): 25cdef7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +97 -49
app.py CHANGED
@@ -1,64 +1,112 @@
 
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
 
 
 
 
 
3
 
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
 
 
 
9
 
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
19
 
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
 
26
- messages.append({"role": "user", "content": message})
27
 
28
- response = ""
 
29
 
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
 
39
- response += token
40
- yield response
41
 
 
 
 
 
 
 
 
 
 
42
 
 
 
 
 
 
 
 
 
43
  """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  """
46
- demo = gr.ChatInterface(
47
- respond,
48
- additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
- gr.Slider(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
56
- step=0.05,
57
- label="Top-p (nucleus sampling)",
58
- ),
59
- ],
60
- )
61
 
 
 
 
 
 
 
 
 
 
 
 
62
 
63
- if __name__ == "__main__":
64
- demo.launch()
 
1
+ import os
2
  import gradio as gr
3
+ from langchain.chains import RetrievalQA
4
+ from langchain.vectorstores import Chroma
5
+ from langchain.llms import OpenAI, HuggingFaceHub
6
+ from langchain.embeddings import OpenAIEmbeddings, HuggingFaceEmbeddings
7
+ from langchain.document_loaders import PyPDFLoader
8
+ import time
9
 
10
+ # Define paths for cybersecurity documents (Add your PDFs here)
11
+ PDF_FILES = ["NIST_CSWP_04162018.pdf", "ISOIEC 27001_2ef522.pdf", "MITRE ATLAS Overview Combined_v1.pdf", "ISO-IEC-27005-2022.pdf"]
 
 
12
 
13
+ # Choose LLM Model (Switch between OpenAI and Hugging Face)
14
+ USE_OPENAI = False # Change to True if you prefer OpenAI API
15
 
16
+ def load_data():
17
+ """Loads multiple PDFs and stores embeddings in ChromaDB"""
18
+ all_docs = []
19
+ for pdf in PDF_FILES:
20
+ if os.path.exists(pdf):
21
+ loader = PyPDFLoader(pdf)
22
+ all_docs.extend(loader.load())
 
 
23
 
24
+ # Use OpenAI or Hugging Face embeddings
25
+ if USE_OPENAI:
26
+ embeddings = OpenAIEmbeddings()
27
+ else:
28
+ embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
29
 
30
+ return Chroma.from_documents(all_docs, embeddings)
31
 
32
+ # Load Vector Database
33
+ vector_db = load_data()
34
 
35
+ # Select LLM model (Online: OpenAI | Offline: Hugging Face)
36
+ if USE_OPENAI:
37
+ llm = OpenAI()
38
+ else:
39
+ llm = HuggingFaceHub(repo_id="google/flan-t5-large", model_kwargs={"temperature": 0.5, "max_length": 512})
 
 
 
40
 
41
+ # Create Retrieval QA chain
42
+ qa_chain = RetrievalQA.from_chain_type(llm=llm, retriever=vector_db.as_retriever())
43
 
44
+ # Function to simulate futuristic typing effect
45
+ def chatbot_response(question):
46
+ """Handles chatbot queries with a typing effect"""
47
+ response = qa_chain.run(question)
48
+ displayed_response = ""
49
+ for char in response:
50
+ displayed_response += char
51
+ time.sleep(0.02) # Simulate typing delay
52
+ yield displayed_response
53
 
54
+ # Custom futuristic CSS style
55
+ custom_css = """
56
+ body {background-color: #0f172a; color: #0ff; font-family: 'Orbitron', sans-serif;}
57
+ #chatbot-container {border: 2px solid #00ffff; background: rgba(0, 0, 0, 0.8); padding: 20px; border-radius: 15px;}
58
+ .gradio-container {background: linear-gradient(to bottom, #020c1b, #001f3f);}
59
+ textarea {background: #011627; color: #0ff; font-size: 18px;}
60
+ button {background: #0088ff; color: white; font-size: 20px; border-radius: 5px; border: none; padding: 10px;}
61
+ button:hover {background: #00ffff; color: #000;}
62
  """
63
+
64
+ # 3D Avatar using Three.js
65
+ three_js_html = """
66
+ <div id="avatar-container">
67
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
68
+ <script>
69
+ function create3DAvatar() {
70
+ var scene = new THREE.Scene();
71
+ var camera = new THREE.PerspectiveCamera(75, 1, 0.1, 1000);
72
+ var renderer = new THREE.WebGLRenderer({ alpha: true });
73
+ renderer.setSize(300, 300);
74
+ document.getElementById('avatar-container').appendChild(renderer.domElement);
75
+
76
+ var geometry = new THREE.SphereGeometry(1, 32, 32);
77
+ var material = new THREE.MeshStandardMaterial({ color: 0x00ffff, wireframe: true });
78
+ var avatar = new THREE.Mesh(geometry, material);
79
+ scene.add(avatar);
80
+
81
+ var light = new THREE.PointLight(0x00ffff, 1, 100);
82
+ light.position.set(2, 2, 5);
83
+ scene.add(light);
84
+
85
+ camera.position.z = 3;
86
+
87
+ function animate() {
88
+ requestAnimationFrame(animate);
89
+ avatar.rotation.y += 0.01;
90
+ renderer.render(scene, camera);
91
+ }
92
+ animate();
93
+ }
94
+ window.onload = create3DAvatar;
95
+ </script>
96
+ </div>
97
  """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98
 
99
+ # Create Gradio Interface with Custom Styling and 3D Avatar
100
+ iface = gr.Interface(
101
+ fn=chatbot_response,
102
+ inputs="text",
103
+ outputs="text",
104
+ title="πŸ€– Cybernetic AI: Your Cybersecurity Assistant",
105
+ description="Ask me about NIST, ISO/IEC 27001, MITRE ATT&CK, and ISO/IEC 27005. Now with a 3D Avatar!",
106
+ theme="default",
107
+ css=custom_css,
108
+ live=True, # Enables real-time updates for typing effect
109
+ )
110
 
111
+ # Embed 3D Avatar into the interface
112
+ iface.launch(share=True, custom_js=three_js_html)