ChatBotsTA commited on
Commit
c3a2b2b
Β·
verified Β·
1 Parent(s): 7cf1e24

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -14
app.py CHANGED
@@ -5,14 +5,17 @@ import pinecone
5
  import qdrant_client
6
  from openai import OpenAI
7
  import graphviz
 
8
 
9
  # =================== CONFIG ===================
10
  OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
11
  PINECONE_API_KEY = os.environ.get("PINECONE_API_KEY")
12
  QDRANT_API_KEY = os.environ.get("QDRANT_API_KEY")
 
13
 
14
  client = OpenAI(api_key=OPENAI_API_KEY)
15
- VECTOR_DB = "qdrant" # change to "pinecone" if needed
 
16
 
17
  # =================== HELPERS ===================
18
  def extract_text_from_pdf(pdf_path):
@@ -22,6 +25,7 @@ def extract_text_from_pdf(pdf_path):
22
  text += page.get_text()
23
  return text
24
 
 
25
  def ingest_text(text, doc_name="doc"):
26
  if VECTOR_DB == "qdrant":
27
  qclient = qdrant_client.QdrantClient(":memory:")
@@ -43,54 +47,84 @@ def ingest_text(text, doc_name="doc"):
43
  index.upsert([(str(0), [0.0]*1536, {"text": text})])
44
  return f"Ingested {len(text.split())} words."
45
 
 
46
  def summarize_text(text):
47
  resp = client.chat.completions.create(
48
  model="gpt-3.5-turbo",
49
- messages=[{"role":"system","content":"Summarize clearly."},
50
- {"role":"user","content":text[:4000]}]
51
  )
52
  return resp.choices[0].message.content
53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  def generate_diagram(text):
55
  dot = graphviz.Digraph()
56
  dot.node("A", "PDF Content")
57
  dot.node("B", "Summary")
58
  dot.node("C", "Key Ideas")
59
- dot.edges([("A","B"),("B","C")])
60
  return dot.pipe(format="png")
61
 
 
62
  def chat_with_pdf(text, question):
63
  resp = client.chat.completions.create(
64
  model="gpt-3.5-turbo",
65
  messages=[
66
- {"role":"system","content":"You are a helpful assistant with access to the document."},
67
- {"role":"user","content":f"Document:\n{text[:3000]}\n\nQuestion:{question}"}
68
  ]
69
  )
70
  return resp.choices[0].message.content
71
 
 
72
  # =================== GRADIO APP ===================
73
  with gr.Blocks(theme="soft") as demo:
74
- gr.Markdown("# πŸ“š PDF Assistant β€” Summarize, Diagram, Chat")
75
-
76
  with gr.Row():
77
  pdf_file = gr.File(label="Upload PDF", file_types=[".pdf"])
78
  doc_name = gr.Textbox(label="Doc name", value="mydoc")
79
-
80
  ingest_btn = gr.Button("πŸš€ Ingest PDF")
81
  ingest_status = gr.Markdown("")
82
-
83
  summary_btn = gr.Button("πŸ“– Summarize")
84
  summary_output = gr.Textbox(label="Summary", lines=8)
85
-
 
 
86
  diagram_btn = gr.Button("πŸ“ Generate Diagram")
87
  diagram_output = gr.Image(type="numpy", label="Diagram Preview")
88
-
89
  with gr.Row():
90
  question = gr.Textbox(label="Ask the PDF a question")
91
  answer = gr.Textbox(label="Answer")
92
  ask_btn = gr.Button("πŸ’¬ Ask")
93
-
94
  pdf_text_state = gr.State("")
95
 
96
  def handle_ingest(pdf_file, doc_name):
@@ -98,8 +132,13 @@ with gr.Blocks(theme="soft") as demo:
98
  status = ingest_text(text, doc_name)
99
  return text, status
100
 
 
 
 
 
 
101
  ingest_btn.click(handle_ingest, [pdf_file, doc_name], [pdf_text_state, ingest_status])
102
- summary_btn.click(lambda t: summarize_text(t), pdf_text_state, summary_output)
103
  diagram_btn.click(lambda t: generate_diagram(t), pdf_text_state, diagram_output)
104
  ask_btn.click(lambda t, q: chat_with_pdf(t, q), [pdf_text_state, question], answer)
105
 
 
5
  import qdrant_client
6
  from openai import OpenAI
7
  import graphviz
8
+ import requests
9
 
10
  # =================== CONFIG ===================
11
  OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
12
  PINECONE_API_KEY = os.environ.get("PINECONE_API_KEY")
13
  QDRANT_API_KEY = os.environ.get("QDRANT_API_KEY")
14
+ ELEVENLABS_API_KEY = os.environ.get("ELEVENLABS_API_KEY")
15
 
16
  client = OpenAI(api_key=OPENAI_API_KEY)
17
+ VECTOR_DB = "qdrant" # change to "pinecone" if you want Pinecone
18
+
19
 
20
  # =================== HELPERS ===================
21
  def extract_text_from_pdf(pdf_path):
 
25
  text += page.get_text()
26
  return text
27
 
28
+
29
  def ingest_text(text, doc_name="doc"):
30
  if VECTOR_DB == "qdrant":
31
  qclient = qdrant_client.QdrantClient(":memory:")
 
47
  index.upsert([(str(0), [0.0]*1536, {"text": text})])
48
  return f"Ingested {len(text.split())} words."
49
 
50
+
51
  def summarize_text(text):
52
  resp = client.chat.completions.create(
53
  model="gpt-3.5-turbo",
54
+ messages=[{"role": "system", "content": "Summarize clearly."},
55
+ {"role": "user", "content": text[:4000]}]
56
  )
57
  return resp.choices[0].message.content
58
 
59
+
60
+ def generate_audio(summary_text):
61
+ if not ELEVENLABS_API_KEY:
62
+ return None
63
+
64
+ url = "https://api.elevenlabs.io/v1/text-to-speech/pnYgVoCjYp9s9v1sXlKS" # default voice
65
+ headers = {
66
+ "xi-api-key": ELEVENLABS_API_KEY,
67
+ "Content-Type": "application/json"
68
+ }
69
+ data = {
70
+ "text": summary_text,
71
+ "voice_settings": {"stability": 0.5, "similarity_boost": 0.7}
72
+ }
73
+ response = requests.post(url, headers=headers, json=data)
74
+
75
+ if response.status_code == 200:
76
+ audio_path = "summary_audio.mp3"
77
+ with open(audio_path, "wb") as f:
78
+ f.write(response.content)
79
+ return audio_path
80
+ else:
81
+ return None
82
+
83
+
84
  def generate_diagram(text):
85
  dot = graphviz.Digraph()
86
  dot.node("A", "PDF Content")
87
  dot.node("B", "Summary")
88
  dot.node("C", "Key Ideas")
89
+ dot.edges([("A", "B"), ("B", "C")])
90
  return dot.pipe(format="png")
91
 
92
+
93
  def chat_with_pdf(text, question):
94
  resp = client.chat.completions.create(
95
  model="gpt-3.5-turbo",
96
  messages=[
97
+ {"role": "system", "content": "You are a helpful assistant with access to the document."},
98
+ {"role": "user", "content": f"Document:\n{text[:3000]}\n\nQuestion: {question}"}
99
  ]
100
  )
101
  return resp.choices[0].message.content
102
 
103
+
104
  # =================== GRADIO APP ===================
105
  with gr.Blocks(theme="soft") as demo:
106
+ gr.Markdown("# πŸ“š PDF Assistant β€” Summarize, Diagram, Audio, Chat")
107
+
108
  with gr.Row():
109
  pdf_file = gr.File(label="Upload PDF", file_types=[".pdf"])
110
  doc_name = gr.Textbox(label="Doc name", value="mydoc")
111
+
112
  ingest_btn = gr.Button("πŸš€ Ingest PDF")
113
  ingest_status = gr.Markdown("")
114
+
115
  summary_btn = gr.Button("πŸ“– Summarize")
116
  summary_output = gr.Textbox(label="Summary", lines=8)
117
+
118
+ audio_output = gr.Audio(label="πŸ”Š Summary Audio")
119
+
120
  diagram_btn = gr.Button("πŸ“ Generate Diagram")
121
  diagram_output = gr.Image(type="numpy", label="Diagram Preview")
122
+
123
  with gr.Row():
124
  question = gr.Textbox(label="Ask the PDF a question")
125
  answer = gr.Textbox(label="Answer")
126
  ask_btn = gr.Button("πŸ’¬ Ask")
127
+
128
  pdf_text_state = gr.State("")
129
 
130
  def handle_ingest(pdf_file, doc_name):
 
132
  status = ingest_text(text, doc_name)
133
  return text, status
134
 
135
+ def handle_summary(text):
136
+ summary = summarize_text(text)
137
+ audio = generate_audio(summary)
138
+ return summary, audio
139
+
140
  ingest_btn.click(handle_ingest, [pdf_file, doc_name], [pdf_text_state, ingest_status])
141
+ summary_btn.click(handle_summary, pdf_text_state, [summary_output, audio_output])
142
  diagram_btn.click(lambda t: generate_diagram(t), pdf_text_state, diagram_output)
143
  ask_btn.click(lambda t, q: chat_with_pdf(t, q), [pdf_text_state, question], answer)
144