ctohereandnowai commited on
Commit
7cf4a0a
·
verified ·
1 Parent(s): 9cce937

initial commit

Browse files
Files changed (3) hide show
  1. app.py +33 -0
  2. branding.json +26 -0
  3. rag_pdf.py +49 -0
app.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from rag_pdf 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"""<div style="display: flex; justify-content: center; margin-bottom: 20px;">
11
+ <img src="{brand_info["logo"]["title"]}" alt="{brand_info["organizationName"]} Logo" style="height: 100px;">
12
+ </div>""")
13
+
14
+ gr.ChatInterface(
15
+ fn=get_response,
16
+ chatbot=gr.Chatbot(height=500,
17
+ avatar_images=(None, brand_info["chatbot"]["avatar"]),
18
+ type="messages"),
19
+ title=brand_info["organizationName"],
20
+ description=brand_info["slogan"],
21
+ type="messages",
22
+ examples=[
23
+ ["who is the cto"],
24
+ ["who is madam Deepti"],
25
+ ["what are the courses offered by Hereandnowai"],
26
+ ]
27
+ )
28
+
29
+ if __name__ == "__main__":
30
+ app.launch()
31
+
32
+
33
+
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
+ }
rag_pdf.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from openai import OpenAI
2
+ from dotenv import load_dotenv
3
+ import os
4
+ import requests
5
+ import PyPDF2
6
+
7
+ load_dotenv()
8
+ api_key = os.getenv("GOOGLE_API_KEY")
9
+ model = "gemini-2.5-flash"
10
+ base_url = "https://generativelanguage.googleapis.com/v1beta/openai"
11
+
12
+ client = OpenAI(base_url=base_url,api_key=api_key)
13
+ url = "https://raw.githubusercontent.com/hereandnowai/rag-workshop/main/pdfs/About_HERE_AND_NOW_AI.pdf"
14
+ response = requests.get(url)
15
+
16
+ script_dir = os.path.dirname(os.path.abspath(__file__))
17
+ file_name= os.path.join(script_dir,"prospect-hereandnowai.pdf")
18
+
19
+ with open(file_name, "wb") as f:
20
+ f.write(response.content)
21
+
22
+ try:
23
+ with open(file_name , "rb") as f:
24
+ reader = PyPDF2.PdfReader(f)
25
+ pdf_text_chunks = []
26
+ for page in reader.pages:
27
+ page_text = page.extract_text()
28
+ if page_text:
29
+ pdf_text_chunks.append(page_text.strip())
30
+ pdf_context = "\n".join(pdf_text_chunks) if pdf_text_chunks else " No text found in pdf"
31
+
32
+ except Exception as e:
33
+ print(f"error no pdf text found {e}")
34
+ pdf_context = "error no pdf text found"
35
+
36
+ system_prompt = f"""content from pdf {file_name}:\n {pdf_context}
37
+ answer the questions based on the context
38
+ if you cannot find the answer say so. dont give wrong information
39
+ """
40
+
41
+ def get_response(usermessage, history):
42
+ messages = [{"role":"system", "content":system_prompt}]
43
+ messages.extend(history)
44
+ messages.append({"role":"user","content":usermessage})
45
+ response = client.chat.completions.create(model=model,messages=messages)
46
+ return response.choices[0].message.content
47
+
48
+ if __name__ == "__main__":
49
+ print(get_response("who is the CTO",[]))