npcJensen commited on
Commit
1835de6
·
verified ·
1 Parent(s): d46b662

Upload 4 files

Browse files
Files changed (4) hide show
  1. app.py +29 -0
  2. branding.json +26 -0
  3. pdf_rag.py +50 -0
  4. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from pdf_rag import get_response
3
+ import json
4
+ import os
5
+
6
+ with open(os.path.abspath(os.path.join(os.path.dirname(__file__), "branding.json"))) as f:
7
+ brand_info = json.load(f)["brand"]
8
+
9
+ with gr.Blocks(theme="default", title=brand_info["organizationName"]) as app:
10
+ gr.HTML(f"""
11
+ <div style="display: flex; justify-content:center; margin-bottom:20px">
12
+ <img src="{brand_info["logo"]["title"]}" alt="{brand_info["organizationName"]} Logo" style="width:200px;height:40px">
13
+ </div> """)
14
+
15
+ gr.ChatInterface(
16
+ fn=get_response,
17
+ chatbot=gr.Chatbot(height=500, avatar_images=(None, brand_info["chatbot"]["avatar"]), type="messages"),
18
+ title=brand_info["organizationName"],
19
+ description=brand_info["slogan"],
20
+ type="messages",
21
+ examples=[
22
+ ["Who is the CEO of Here and Now AI?"],
23
+ ["What is the mission of Here and Now AI?"],
24
+ ["Tell me about the team behind Here and Now AI."]
25
+ ]
26
+ )
27
+
28
+ if __name__ == "__main__":
29
+ app.launch(share=True)
branding.json ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "brand":
3
+ {
4
+ "organizationName": "HERE AND NOW AI",
5
+ "website": "https://hereandnowai.com",
6
+ "email": "info@hereandnowai.com",
7
+ "mobile": "+91 996 296 1000",
8
+ "slogan": "designed with passion for innovation",
9
+ "colors": {"primary": "#FFDF00", "secondary": "#004040"},
10
+ "logo":
11
+ {
12
+ "title": "https://raw.githubusercontent.com/hereandnowai/images/refs/heads/main/logos/logo-of-here-and-now-ai.png",
13
+ "favicon": "https://raw.githubusercontent.com/hereandnowai/images/refs/heads/main/logos/favicon-logo-with-name.png"}, "chatbot": {"avatar": "https://raw.githubusercontent.com/hereandnowai/images/refs/heads/main/logos/caramel.jpeg", "face": "https://raw.githubusercontent.com/hereandnowai/images/refs/heads/main/logos/caramel-face.jpeg"
14
+ },
15
+
16
+ "socialMedia":
17
+ {
18
+ "blog": "https://hereandnowai.com/blog",
19
+ "linkedin": "https://www.linkedin.com/company/hereandnowai/",
20
+ "instagram": "https://instagram.com/hereandnow_ai",
21
+ "github": "https://github.com/hereandnowai",
22
+ "x": "https://x.com/hereandnow_ai",
23
+ "youtube": "https://youtube.com/@hereandnow_ai"
24
+ }
25
+ }
26
+ }
pdf_rag.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from openai import OpenAI
2
+ from dotenv import load_dotenv
3
+ import os
4
+ import requests
5
+ import PyPDF2
6
+
7
+
8
+ load_dotenv()
9
+ api_key = os.getenv("GEMINI_API_KEY")
10
+ model="gemini-2.5-flash-lite"
11
+ base_url = "https://generativelanguage.googleapis.com/v1beta/openai/"
12
+ client = OpenAI(base_url=base_url, api_key=api_key)
13
+
14
+ url="https://raw.githubusercontent.com/hereandnowai/sathyabama-be-cse-ai-pt1-07-2025-hands-on-professional-training-on-genai-and-ai-agents/main/general-profile-of-hereandnowai.pdf"
15
+ response = requests.get(url)
16
+
17
+ PDF_FILE_NAME ="profile-of-hereandnowai.pdf"
18
+ PDF_DIR = os.path.dirname(__file__)
19
+ PDF_PATH = os.path.join(PDF_DIR, PDF_FILE_NAME)
20
+
21
+ with open(PDF_PATH, "wb") as f:
22
+ f.write(response.content)
23
+
24
+ try:
25
+ with open(PDF_PATH, "rb") as file:
26
+ reader = PyPDF2.PdfReader(file)
27
+ pdf_text_chunks = []
28
+ for page in reader.pages:
29
+ page_text = page.extract_text()
30
+ if page_text:
31
+ pdf_text_chunks.append(page_text.strip())
32
+ pdf_context = "\n".join(pdf_text_chunks) if pdf_text_chunks else "Error extracting text from PDF file."
33
+ except Exception as e:
34
+ print(f"Error reading the PDF file: {e}")
35
+ pdf_context = "Error extracting text from PDF file."
36
+
37
+
38
+
39
+ def get_response(message, history):
40
+ system_prompt = f"""context from {PDF_PATH}:\n{pdf_context}
41
+ \n\n Qustion:{message}
42
+ \n\n Answer based only on the context:"""
43
+ messages = [{"role": "system", "content": system_prompt}]
44
+ messages.extend(history)
45
+ messages.append({"role": "user", "content": message})
46
+ response = client.chat.completions.create(model=model, messages=messages)
47
+ ai_response = response.choices[0].message.content
48
+ return ai_response
49
+
50
+ print(get_response("who is the ceo of here and now ai?", []))
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ openai
2
+ python-dotenv
3
+ gradio
4
+ PyPDF2