-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
740 lines (570 loc) · 34.2 KB
/
app.py
File metadata and controls
740 lines (570 loc) · 34.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
from math import e
from fastapi import FastAPI, Path, HTTPException, Query
from fastapi.responses import JSONResponse
import json
from pydantic import BaseModel, Field, field_validator, computed_field, AnyUrl, EmailStr
from typing import Annotated, Literal, Optional, List, Dict
import subprocess
from pathlib import Path as pathOfPathLib
from jinja2 import Environment, FileSystemLoader
from google import genai
from groq import Groq
import os
import httpx
import re
from dotenv import load_dotenv
load_dotenv()
# API section
gemClient = genai.Client(
api_key=os.getenv('gemAPI1')
)
GroqClient = Groq(
api_key=os.getenv('groqAPI2')
)
CORE_API_KEY = os.getenv('coreAPI3')
app = FastAPI()
class GeminiRequest(BaseModel):
prompt: Annotated[str, Field(..., title="Here, we can use Gemini", description="Enter prompt for the Gemini to compute....")]
class GroqRequest(BaseModel):
prompt: Annotated[str, Field(..., title="Here, we can use Groq", description="Enter prompt for the Groq to compute....")]
class CoreRequest(BaseModel):
prompt: Annotated[str, Field(..., title="Here, we can use Core for Research pages", description="Enter prompt for the Core to compute....")]
# class ContentItem(BaseModel):
# subContent: str
# references: str
# class ContentBlock(BaseModel):
# content: dict[str, ContentItem]
# class PulsusOutputStr(BaseModel):
# content: Annotated[ContentBlock,Field(..., title="This is the content block", description="Enter the stacks in the content blocks....")]
class PulsusInputStr(BaseModel):
id: Annotated[str, Field(..., title="ID of the Input Journal", description="Enter the id for this journal input....")]
topic: Annotated[str, Field(..., title="Name of the topic", description="Enter the topic....")]
journalName : Annotated[str, Field(..., title="Name of the journal where it belongs to.", description="Enter the journal where it belongs from...")]
shortJournalName : Annotated[str, Field(..., title="Name of the short journal name where it belongs to.", description="Enter the short journal name where it belongs from...")]
type: Annotated[str, Field(..., title="Name of the type(journal)", description="Enter the type of journal....")]
citation : Annotated[str, Field(..., title="The citation of the journal", description="Enter the citation for this journal....")]
author: Annotated[str, Field(..., title="Name of the author", description="Enter the author....")]
email : Annotated[EmailStr, Field(..., title="Email of the author", description="Enter the autors email....")]
authorsDepartment : Annotated[str, Field(..., title="Department of the authour", description="Enter the department of the author....")]
received: Annotated[str, Field(..., title="The receiving date", description="Enter the receiving date in DD-Mon format....")]
editorAssigned: Annotated[str, Field(..., title="The Editor Assigned date", description="Enter the editor assigned date in DD-Mon format....")]
reviewed: Annotated[str, Field(..., title="The journal review date", description="Enter the journal review date in DD-Mon format....")]
revised: Annotated[str, Field(..., title="The journal revised date", description="Enter the journal revised date in DD-Mon format....")]
published: Annotated[str, Field(..., title="The publishing date of journal", description="Enter the publishing date of the journal in DD-Mon format....")]
manuscriptNo : Annotated[str, Field(..., title="The manuscriptNo of this journal", description="Enter the manuscriptNo for this journal....")]
volume: Annotated[int, Field(..., title="The volume for the issue", description="Enter the Volume of the issue...", gt=0)]
issues: Annotated[int, Field(..., title="The issue no. of the volume", description="Enter the issue no. of the volume...",gt=0)]
pdfNo: Annotated[int, Field(..., title="The pdf number", description="Enter the pdf number....",gt=0)]
doi : Annotated[str, Field(..., title="DOI for this journal", description="Enter DOI for this Journal....")]
ISSN : Annotated[str, Field(..., title="ISSN number of this journal", description="Enter the ISSN number for the journal....")]
parentLink : Annotated[AnyUrl, Field(..., title="The url for the centralized link", description="Enter the link which will led to the centralized page....")]
@field_validator('received', "editorAssigned", "reviewed", "revised", "published")
@classmethod
def validateDates(cls, value):
months = {'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'}
newVal = value.split("-")
if len(newVal) != 3:
raise ValueError("Enter Correct date formate DD-Mmm-YYYY")
newVal[1] = newVal[1].capitalize()
if newVal[1] not in months:
raise ValueError("Enter the correct months['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']")
if int(newVal[0])>31 or int(newVal[0])<1:
raise ValueError("invalid date, keep that inbetween 1-31")
value = '-'.join(newVal)
return value
@field_validator('pdfNo')
@classmethod
def validatePDFNo(cls, value):
data = fetchInpData()
for i in data.values():
if value == i['pdfNo']:
raise ValueError(f"Change the pdf page no. it is similar to the pdf artical name{i['topic']}")
return value
class UpdateInputPartJournal(BaseModel):
id: Annotated[Optional[str], Field(default=None, title="ID of the Input Journal", description="Enter the id for this journal input....")]
topic: Annotated[Optional[str], Field(default=None, title="Name of the topic", description="Enter the topic....")]
journalName : Annotated[Optional[str], Field(default=None, title="Name of the journal where it belongs to.", description="Enter the journal where it belongs from...")]
shortJournalName : Annotated[Optional[str], Field(default=None, title="Name of the short journal name where it belongs to.", description="Enter the short journal name where it belongs from...")]
type: Annotated[Optional[str], Field(default=None, title="Name of the type(journal)", description="Enter the type of journal....")]
citation : Annotated[Optional[str], Field(default=None, title="The citation of the journal", description="Enter the citation for this journal....")]
author: Annotated[Optional[str], Field(default=None, title="Name of the author", description="Enter the author....")]
email : Annotated[Optional[EmailStr], Field(default=None, title="Email of the author", description="Enter the autors email....")]
authorsDepartment : Annotated[Optional[str], Field(default=None, title="Department of the authour", description="Enter the department of the author....")]
received: Annotated[Optional[str], Field(default=None, title="The receiving date", description="Enter the receiving date in DD-Mon format....")]
editorAssigned: Annotated[Optional[str], Field(default=None, title="The Editor Assigned date", description="Enter the editor assigned date in DD-Mon format....")]
reviewed: Annotated[Optional[str], Field(default=None, title="The journal review date", description="Enter the journal review date in DD-Mon format....")]
revised: Annotated[Optional[str], Field(default=None, title="The journal revised date", description="Enter the journal revised date in DD-Mon format....")]
published: Annotated[Optional[str], Field(default=None, title="The publishing date of journal", description="Enter the publishing date of the journal in DD-Mon format....")]
manuscriptNo : Annotated[Optional[str], Field(default=None, title="The manuscriptNo of this journal", description="Enter the manuscriptNo for this journal....")]
volume: Annotated[Optional[int], Field(default=None, title="The volume for the issue", description="Enter the Volume of the issue...", gt=0)]
issues: Annotated[Optional[int], Field(default=None, title="The issue no. of the volume", description="Enter the issue no. of the volume...",gt=0)]
pdfNo: Annotated[Optional[int], Field(default=None, title="The pdf number", description="Enter the pdf number....",gt=0)]
doi : Annotated[Optional[str], Field(default=None, title="DOI for this journal", description="Enter DOI for this Journal....")]
ISSN : Annotated[Optional[str], Field(default=None, title="ISSN number of this journal", description="Enter the ISSN number for the journal....")]
parentLink : Annotated[Optional[AnyUrl], Field(default=None, title="The url for the centralized link", description="Enter the link which will led to the centralized page....")]
class ArticleItem(BaseModel):
title: Annotated[Optional[str], Field(default=None, title="Name of the topic", description="Enter the topic....")]
subContent: Annotated[Optional[str], Field(default=None, title="Name of the topic", description="Enter the topic....")]
authors: Annotated[Optional[str], Field(default=None, title="Name of the topic", description="Enter the topic....")]
published: Annotated[Optional[str], Field(default=None, title="Name of the topic", description="Enter the topic....")]
doi: Annotated[Optional[str], Field(default=None, title="Name of the topic", description="Enter the topic....")]
url: Annotated[Optional[AnyUrl], Field(default=None, title="Name of the topic", description="Enter the topic....")]
fulltextLinks: Annotated[Optional[List[str]], Field(default=None, title="Name of the topic", description="Enter the topic....")]
keywords: Annotated[Optional[str], Field(default=None, title="Name of the topic", description="Enter the topic....")]
references: Annotated[Optional[str], Field(default=None, title="Name of the topic", description="Enter the topic....")]
class PulsusOutputStr(BaseModel):
title : Annotated[str, Field(..., title="ID of the Input Journal", description="Enter the id for this journal input....")]
journalName : Annotated[str, Field(..., title="ID of the Input Journal", description="Enter the id for this journal input....")]
shortJournalName : Annotated[str, Field(..., title="Name of the short journal name where it belongs to.", description="Enter the short journal name where it belongs from...")]
type : Annotated[str, Field(..., title="ID of the Input Journal", description="Enter the id for this journal input....")]
authors : Annotated[str, Field(..., title="ID of the Input Journal", description="Enter the id for this journal input....")]
email : Annotated[EmailStr, Field(..., title="ID of the Input Journal", description="Enter the id for this journal input....")]
authorsDepartment : Annotated[str, Field(..., title="ID of the Input Journal", description="Enter the id for this journal input....")]
citation : Annotated[str, Field(..., title="ID of the Input Journal", description="Enter the id for this journal input....")]
journalYearVolumeIssue : Annotated[str, Field(..., title="ID of the Input Journal", description="Enter the id for this journal input....")]
introduction : Annotated[str, Field(..., title="ID of the Input Journal", description="Enter the id for this journal input....")]
description : Annotated[str, Field(..., title="ID of the Input Journal", description="Enter the id for this journal input....")]
content : Annotated[Dict[str, Dict], Field(..., title="ID of the Input Journal", description="Enter the id for this journal input....")]
doi : Annotated[str, Field(..., title="ID of the Input Journal", description="Enter the id for this journal input....")]
received : Annotated[str, Field(..., title="ID of the Input Journal", description="Enter the id for this journal input....")]
editorAssigned : Annotated[str, Field(..., title="ID of the Input Journal", description="Enter the id for this journal input....")]
reviewed : Annotated[str, Field(..., title="ID of the Input Journal", description="Enter the id for this journal input....")]
revised : Annotated[str, Field(..., title="ID of the Input Journal", description="Enter the id for this journal input....")]
published : Annotated[str, Field(..., title="ID of the Input Journal", description="Enter the id for this journal input....")]
year : Annotated[int, Field(..., title="Yes of publishing", description="Enter the journal publising year...")]
manuscriptNo : Annotated[str, Field(..., title="ID of the Input Journal", description="Enter the id for this journal input....")]
QCNo : Annotated[str, Field(..., title="The QC number", description="Enter the QC number....")]
preQCNo : Annotated[str, Field(..., title="The preQC number", description="Enter the preQC number....")]
RManuNo : Annotated[str, Field(..., title="The RManuNo number", description="Enter the RManuNo number....")]
volume: Annotated[int, Field(..., title="The volume for the issue", description="Enter the Volume of the issue...", gt=0)]
issues: Annotated[int, Field(..., title="The issue no. of the volume", description="Enter the issue no. of the volume...",gt=0)]
ISSN : Annotated[str, Field(..., title="ISSN Number", description="Enter the ISSN Number....")]
parentLink : Annotated[AnyUrl, Field(..., title="ID of the Input Journal", description="Enter the id for this journal input....")]
conclusion : Annotated[str, Field(..., title="ID of the Input Journal", description="Enter the id for this journal input....")]
@field_validator('content')
@classmethod
def validatePDFNo(cls, value):
# Avoid modifying dict during iteration
keys_to_delete = []
for i, j in value.items():
if j.get('subContent') is None or j.get('references') is None:
keys_to_delete.append(i)
for k in keys_to_delete:
del value[k]
return value
def fetchInpData():
if not os.path.exists('journalDBInput.json'):
return {} # File doesn't exist → return empty dict
with open('journalDBInput.json', 'r') as file:
try:
data = json.load(file)
if not isinstance(data, dict): # Ensure it's a dict
return {}
return data
except json.JSONDecodeError:
# File exists but is empty or invalid JSON
return {}
def saveInpData(data):
with open('journalDBInput.json','w') as file:
if data is None:
file = {}
json.dump(data, file, default=str)
def fetchOutData():
if not os.path.exists('journalDBOutput.json'):
return {} # File doesn't exist → return empty dict
with open('journalDBOutput.json', 'r') as file:
try:
data = json.load(file)
if not isinstance(data, dict): # Ensure it's a dict
return {}
return data
except json.JSONDecodeError:
# File exists but is empty or invalid JSON
return {}
def saveOutData(data):
with open('journalDBOutput.json','w') as file:
if data is None:
file = {}
json.dump(data, file, default=str)
def sanitize_for_json(obj):
"""Convert objects like AnyUrl into plain serializable types."""
if isinstance(obj, dict):
return {k: sanitize_for_json(v) for k, v in obj.items()}
elif isinstance(obj, list):
return [sanitize_for_json(v) for v in obj]
elif isinstance(obj, AnyUrl):
return str(obj)
return obj
def extract_json_from_markdown(text: str) -> str:
"""Extract JSON block from LLM markdown-style response."""
match = re.search(r"```(?:json)?\s*(\{.*?\})\s*```", text, re.DOTALL)
if match:
return match.group(1)
return text.strip()
@app.get("/")
def home():
return {"message":"Automate the journals"}
@app.get("/about")
def aboutMe():
return {"message":"This is a process where we are going to work with some Transformers APIs and that gonna lead us to a automation(by webscraping and more.)"}
@app.get("/view/journalInputData")
def viewTheData():
data = fetchInpData()
return f'{data}\n this is the journal data input.'
@app.get("/journalInputData/{JournalInputID}")
def fetchOneP(JournalInputID: str = Path(..., description='Enter your journal input index here....', example="J001",max_length=4)):
data = fetchInpData()
if JournalInputID in data:
return data[JournalInputID]
raise HTTPException(status_code=404, detail='JOurnal input not found in DB')
@app.post("/addJournalInInput")
def createJournal(pulsusInput : PulsusInputStr):
data = fetchInpData()
if pulsusInput.id in data:
raise HTTPException(status_code=400, detail="The id is already there.")
data[pulsusInput.id] = pulsusInput.model_dump(exclude=["id"])
saveInpData(data)
return JSONResponse(status_code=200, content="Data Added successfully")
@app.put("/updateInputJournal/{JournalInputID}")
def updateInpJournal(JournalInputID: str, updateJor : UpdateInputPartJournal):
data = fetchInpData()
if JournalInputID not in data:
raise HTTPException(status_code=404, detail="Journal Input not found")
tempStoreInfo = data[JournalInputID]
tempStoreInfo["id"] = JournalInputID
updatedInfo = updateJor.model_dump(exclude_unset=True)
for key, value in updatedInfo.items():
tempStoreInfo[key] = value
validateInpJournal = UpdateInputPartJournal(**tempStoreInfo)
data[JournalInputID] = validateInpJournal.model_dump(exclude=['id'])
saveInpData(data)
return JSONResponse(status_code=200,content={"message":"Successfully updated"})
@app.delete("/delete/journalInputData/{JournalInputID}")
def deletePatient(JournalInputID):
data = fetchInpData()
if JournalInputID not in data:
raise HTTPException(status_code=404, detail="Journal Not Found")
del data[JournalInputID]
saveInpData(data)
return JSONResponse(status_code=200, content={"message":f"Perfectly deleted the {JournalInputID}"})
@app.post("/pulsus-ask-gemini")
def pulsus_ask_gemini(gem: GeminiRequest):
try:
response = gemClient.models.generate_content(
model="gemini-2.5-flash", contents=gem.prompt
)
return {"response": response.text}
except Exception as e:
raise HTTPException(status_code=500, detail=f"Gemini API error: {str(e)}")
@app.post("/pulsus-ask-groq")
def pulsus_ask_groq(LLaMAAAAAAA : GroqRequest):
try:
chat_completion = GroqClient.chat.completions.create(
messages=[
{
"role": "user",
"content": LLaMAAAAAAA.prompt,
}
],
model="llama-3.3-70b-versatile",
)
return {"response": chat_completion.choices[0].message.content}
except Exception as e:
raise HTTPException(status_code=500, detail=f"Groq API error: {str(e)}")
def build_structured_content(results):
content = {}
for idx, item in enumerate(results.get("results", []), start=1):
key = f"C{str(idx).zfill(3)}"
title = item.get("title", "No title available")
abstract = item.get("abstract", "No abstract available.")
authors = ", ".join([a.get("name", "Unknown") for a in item.get("authors", [])]) or "Unknown author(s)"
published_date = item.get("published", "Unknown date")
doi = item.get("doi", "DOI not available")
url = item.get("url", "https://URLNotAvailable")
fulltext_links = item.get("fulltextUrls", [])
subjects = ", ".join(item.get("topics", [])) if item.get("topics") else "No keywords"
source = item.get("source", {}).get("name", "Unknown source")
references = f"{authors} ({published_date}). {title}. {source}. DOI: {doi}"
content[key] = {
"title": title,
"subContent": abstract,
"authors": authors,
"published": published_date,
"doi": doi,
"url": url,
"fulltextLinks": fulltext_links,
"keywords": subjects,
"references": references
}
tempStoreContentForAuth = ArticleItem(**content[key])
content[key] = tempStoreContentForAuth.model_dump()
return {"content": content}
@app.post("/core/search/articles")
async def search_articles(core_req: CoreRequest):
CORE_API_URL = "https://api.core.ac.uk/v3/search/works"
headers = {
"Authorization": f"Bearer {CORE_API_KEY}",
"Content-Type": "application/json"
}
data = {
"q": core_req.prompt,
"limit": 20
}
try:
async with httpx.AsyncClient(timeout=15.0) as client: # set timeout explicitly
response = await client.post(CORE_API_URL, json=data, headers=headers)
response.raise_for_status()
try:
results = response.json()
except Exception as json_err:
raise HTTPException(status_code=500, detail=f"Invalid JSON in response: {str(json_err)}")
return build_structured_content(results)
except httpx.HTTPStatusError as e:
raise HTTPException(status_code=e.response.status_code,
detail=f"CORE API returned HTTP error: {e.response.text}")
except httpx.RequestError as e:
raise HTTPException(status_code=500, detail=f"Request error: {str(e)}")
except Exception as e:
raise HTTPException(status_code=500, detail=f"Unexpected error: {str(e)}")
@app.post("/pipeline/journal-full-process")
async def full_journal_pipeline(journal: PulsusInputStr):
# Step 1: Save journal input
data = fetchInpData()
if journal.id in data:
raise HTTPException(status_code=400, detail="Journal ID already exists.")
data[journal.id] = journal.model_dump(exclude=["id"])
# # Step 2: Use the topic to search CORE articles
# CORE_API_URL = "https://api.core.ac.uk/v3/search/works"
# headers = {
# "Authorization": f"Bearer {CORE_API_KEY}",
# "Content-Type": "application/json"
# }
# data = {
# "q": journal.topic,
# "limit": 1000
# }
# try:
# async with httpx.AsyncClient(timeout=15.0) as client: # set timeout explicitly
# response = await client.post(CORE_API_URL, json=data, headers=headers)
# response.raise_for_status()
# try:
# results = response.json()
# except Exception as json_err:
# raise HTTPException(status_code=500, detail=f"Invalid JSON in response: {str(json_err)}")
# core_content_json = build_structured_content(results)
# except httpx.HTTPStatusError as e:
# raise HTTPException(status_code=e.response.status_code,
# detail=f"CORE API returned HTTP error: {e.response.text}")
# except httpx.RequestError as e:
# raise HTTPException(status_code=500, detail=f"Request error: {str(e)}")
# except Exception as e:
# raise HTTPException(status_code=500, detail=f"Unexpected error: {str(e)}")
# Step 3: Create universal prompt
prompt = f"""
You are provided by a topic:
topic : "{journal.topic}"
Using this data, generate a summarized structure that contains only "subContent" (summary of key insights from the article) and "references" (citation-style reference).
The final structure should look like:
"content": {{
"C001": {{
"subContent": "...",
"references": "...",
"parentLink": "..." #parent link is the link where we can find the article or the journal
}}, # try to achieve as much as possible but maximum will be 8(C008) and the minimum will be 5(C005)
...
}}
Ensure the summaries are meaningful and extracted from the provided article data.
Focus on creating references from title, authors, year, and DOI if available.
And also if possible add some more important ("article" and "journal") data on that topic from your side and merge it with the same thing.
the most important the whole data will be copied out and used so give me clean information only the structured data no other thing not even a symbol or dot.
note: Write like a confident, clear thinking human speaking to another smart human.
Avoid robotic phrases like 'in today's fast-paced world', 'leveraging synergies', or
'furthermore.
Skip unnecessary dashes (-), quotation marks (''), and corporate buzzwords like 'cutting-edge', 'robust', or 'seamless experience. No Al tone. No fluff. No filler.
Use natural transitions like 'here's the thing', 'let's break it down; or 'what this really means is' Keep sentences varied in length and rhythm, like how real people speak or write. Prioritize clarity, personality, and usefulness.
Every sentence should feel intentional, not generated
"""
# Step 4: Ask Gemini
try:
gem_response = gemClient.models.generate_content(
model="gemini-2.5-flash", contents=prompt
)
gem_summary = gem_response.text
except Exception as e:
gem_summary = f"Gemini API failed: {str(e)}"
# Step 5: Ask Groq
try:
chat_completion = GroqClient.chat.completions.create(
messages=[{"role": "user", "content": prompt}],
model="llama-3.3-70b-versatile",
)
groq_summary = chat_completion.choices[0].message.content
except Exception as e:
groq_summary = f"Groq API failed: {str(e)}"
# Step 6: Clean and parse JSON output from Gemini or Groq
raw_json = extract_json_from_markdown(gem_summary if "Gemini API failed" not in gem_summary else groq_summary)
try:
parsed = json.loads(raw_json)
content_data = parsed["content"]
except Exception as e:
raise HTTPException(status_code=500, detail=f"Failed to parse structured JSON from LLM output: {str(e)}")
# Step 7: Conclusion content using Gemini
prompt = f"""
This is the given data : "{content_data}"
i want to you to process this data and give me some output:
1: Give me a brief summary from the given data where the word count lies in between 200 - 400.
2: Give me a brief introduction from the given data where it will contain the citation markers as well, and you have to take in this way: the "C001" will be 1, "C002": 2......, where the word count lies in between 600 - 800.
3: Give me a brief description from the given data where it will contain the citation markers as well, and you have to take in this way: the "C001" will be 1, "C002": 2......, where the word count lies in between 600 - 800.
The final structure should look like:
"content": {{
"introduction": '''...''',
"description" : '''...''',
"summary" : '''...'''
...
}}
note: Do not include any introductory labels, brand names, or meta-commentary. Remove all special characters, escape sequences, and formatting symbols. Respond only with plain and clean text containing the summary. Respond without any introductory phrases, labels, brand mentions, or headings (e.g., 'Summary:', 'Gemini:', 'Groq:'). Do not include explanations of how you generated the answer unless explicitly asked.
Write like a confident, clear thinking human speaking to another smart human.
Avoid robotic phrases like 'in today's fast-paced world', 'leveraging synergies', 'furthermore'.....
Skip unnecessary dashes (-), quotation marks (''), and corporate buzzwords like 'cutting-edge', 'robust', or 'seamless experience. No Al tone. No fluff. No filler.
Use natural transitions like 'here's the thing', ‘let's break it down; or ‘what this really means is’ Keep sentences varied in length and rhythm, like how real people speak or write. Prioritize clarity, personality, and usefulness.
Every sentence should feel intentional, not generated
"""
try:
gem_response = gemClient.models.generate_content(
model="gemini-2.5-flash", contents=prompt
)
gem_info = gem_response.text
except Exception as e:
gem_info = f"Gemini API failed: {str(e)}"
# Step 7.5: Clean and parse JSON output from Gemini or Groq
raw_json = extract_json_from_markdown(gem_info)
try:
parsed = json.loads(raw_json)
gem_info = parsed["content"]
except Exception as e:
raise HTTPException(status_code=500, detail=f"Failed to parse structured JSON from LLM output: {str(e)}")
# Step 8: Title content using Gemini
prompt = f"give me a 5 to 7 words title based on the generated summary {content_data}. use playoff method to generate 5,6 titles and choose the best one and give that title. no need to display background process. just give 1 title as a final response"
try:
gem_response = gemClient.models.generate_content(
model="gemini-2.5-flash", contents=prompt
)
gem_title = gem_response.text
except Exception as e:
gem_title = f"Gemini API failed: {str(e)}"
# Step 9: Final response
final_output = {
journal.id: {
"title": gem_title,
"journalName" : journal.journalName,
"shortJournalName" : journal.shortJournalName,
"type": journal.type,
"authors": journal.author,
"email": journal.email,
"authorsDepartment": journal.authorsDepartment,
"citation": journal.citation,
"journalYearVolumeIssue": f"{journal.shortJournalName}, Volume {journal.volume}:{journal.issues}, {journal.published.split('-')[-1]}",
"introduction": gem_info["introduction"] ,
"description": gem_info["description"] ,
"content": content_data,
"doi": journal.doi,
"received": journal.received,
"editorAssigned": journal.editorAssigned,
"reviewed": journal.reviewed,
"revised": journal.revised,
"published": journal.published,
"year" : int(journal.published.split('-')[-1]),
"manuscriptNo": journal.manuscriptNo,
"QCNo": f"Q-{journal.manuscriptNo.split('-')[-1]}",
"preQCNo": f"P-{journal.manuscriptNo.split('-')[-1]}",
"RManuNo" : f"R-{journal.manuscriptNo.split('-')[-1]}",
"volume" : journal.volume,
"issues" : journal.issues,
"ISSN" : journal.ISSN,
"parentLink": str(journal.parentLink),
"conclusion": gem_info["summary"]
}
}
saveInpData(data)
data = fetchOutData()
pulsus_output_instance = PulsusOutputStr(**final_output[journal.id])
data[journal.id] = pulsus_output_instance.model_dump()
saveOutData(data)
#10 : create HTML file
env = Environment(
loader=FileSystemLoader(pathOfPathLib(".")) # Assumes templates are in the current folder
)
# ===== Render and Save HTML File =====
try:
html_template = env.get_template("journal_template.html")
rendered_html = html_template.render(**data[journal.id])
html_output_filename = f"{journal.id}.html"
html_file = pathOfPathLib(html_output_filename)
html_file.write_text(rendered_html, encoding="utf-8")
except Exception as e:
# It's good practice to handle potential errors, e.g., template not found
raise HTTPException(status_code=500, detail=f"Failed to generate HTML file: {str(e)}")
#11 : create PDF file
# ===== Load Jinja2 Template =====
env = Environment(
block_start_string=r'\BLOCK{',
block_end_string='}',
variable_start_string=r'\VAR{',
variable_end_string='}',
comment_start_string=r'\#{',
comment_end_string='}',
line_statement_prefix='%%',
line_comment_prefix='%#',
trim_blocks=True,
autoescape=False,
loader=FileSystemLoader(pathOfPathLib(".")) # current folder
)
def latex_escape(text):
if not isinstance(text, str):
return text
replacements = {
'&': r'\&',
'%': r'\%',
'$': r'\$',
'#': r'\#',
'_': r'\_',
'{': r'\{',
'}': r'\}',
'^': r'\^{}',
}
pattern = re.compile('|'.join(re.escape(k) for k in replacements.keys()))
return pattern.sub(lambda m: replacements[m.group()], text)
env.filters['latex_escape'] = latex_escape
template = env.get_template("finalFormate.tex")
# ===== Render LaTeX =====
rendered_latex = template.render(**data[journal.id])
outputFileName = f"{journal.id}.tex"
# ===== Save LaTeX to file =====
tex_file = pathOfPathLib(outputFileName)
tex_file.write_text(rendered_latex, encoding="utf-8")
# ===== Compile LaTeX to PDF =====
for i in range(2): # run twice so references and page labels are resolved
result = subprocess.run(
["xelatex", "-interaction=nonstopmode", str(tex_file)],
text=True
)
if result.returncode != 0:
# Save full log for later debugging
log_file = tex_file.with_suffix(".log")
log_file.write_text(result.stdout + "\n" + result.stderr)
# Extract only lines containing "!" (LaTeX errors) and a few lines after each
lines = result.stdout.splitlines()
error_snippets = []
for j, line in enumerate(lines):
if line.startswith("!"):
snippet = "\n".join(lines[j:j+6]) # error line + context
error_snippets.append(snippet)
error_text = "\n\n".join(error_snippets) or "Unknown LaTeX error."
raise HTTPException(
status_code=500,
detail=f"LaTeX compilation failed on run {i+1}:\n\n{error_text}\n\n(Full log saved to {log_file})"
)
return JSONResponse(
status_code=200,
content="Data added successfully and generated PDF successfully ✅."
)