-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: New download function with improved chatbot interface and implem…
…entation of videos and lifestyle recommendation implemented fixed the #5
- Loading branch information
1 parent
1c6bdfa
commit 002d64f
Showing
6 changed files
with
63 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,51 @@ | ||
import os | ||
import nltk | ||
from nltk.tokenize import sent_tokenize | ||
from weasyprint import HTML | ||
import logging | ||
import pandas as pd | ||
from logger import logger | ||
from diet import recommend_Diet | ||
from weasyprint import HTML, CSS | ||
from nltk.tokenize import sent_tokenize | ||
from typing import Literal | ||
import os | ||
|
||
df = pd.read_csv(os.path.join("dataset", "videos.csv")) | ||
# nltk.download("punkt") | ||
|
||
logging.getLogger("weasyprint").setLevel(logging.WARNING) | ||
logging.getLogger("fontTools").setLevel(logging.WARNING) | ||
|
||
def createHTMLListString(text: str) -> str: | ||
"""Converts the paragraph into a Unorderedlist HTML Tag. | ||
Args: | ||
text (str): Paragraph to be converted into a unorderedlist. | ||
Returns: | ||
str: Paragraph converted into a unorderedlist. | ||
""" | ||
def createHTMLListString(text: str, listTag: Literal["<ul>", "<ol>"], closingTag: Literal["</ul>", "</ol>"]) -> str: | ||
text: list[str] = sent_tokenize(text) | ||
text.insert(0, "<ul>") | ||
text.insert(len(text), "</ul>") | ||
text.insert(0, listTag) | ||
text.insert(len(text), closingTag) | ||
for i in range(1, len(text)-1): | ||
text[i] = f"<li>{text[i]}</li>" | ||
text = "".join(text) | ||
return text | ||
|
||
|
||
def createPDF(consume: str, avoid: str, dosha: str) -> bytes: | ||
html_content = f""" | ||
<html> | ||
<head> | ||
<title>Your Prakriti - AyurInsights</title> | ||
</head> | ||
<body> | ||
<header> | ||
<div> | ||
<center> | ||
<img src="https://ucarecdn.com/56c57686-f88c-471d-8b0e-f7b6b70ee7a4/-/preview/300x189/" alt="Logo"> | ||
</center> | ||
</div> | ||
</header> | ||
<div> | ||
<h2 style="text-align: center;">Your Prakriti is : {dosha}</h2> | ||
<div class="table-result"> | ||
<div style="font-size: 20px;font-weight: bold;text-align: center;text-decoration-line: underline;">Diet | ||
Recommendation</div> | ||
<div> | ||
<h3>To Consume: </h6> | ||
<div style="text-align: justify;"> | ||
{createHTMLListString(consume)} | ||
</div> | ||
</div> | ||
<div> | ||
<h3>To Avoid: </h3> | ||
<div style="text-align: justify;"> | ||
{createHTMLListString(avoid)} | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
<footer style="padding-top: 17px;"> | ||
<div style="padding:14px 0px; color:gray; text-align: center;"> | ||
<b>Disclaimer : </b>The dietary recommendations provided above have been curated and tested by Ayurvedic | ||
experts. | ||
</div> | ||
<div style="padding: 8px;border: 2px solid black ;"> | ||
<b>Note:</b> This document has been automatically generated by <a | ||
href="http://localhost:5173">AyurInsights</a>. You can also discover your prakriti by visiting and | ||
answering a set of questions. | ||
</div> | ||
<div style="text-align: center;padding-top: 18px;font-weight: bold;font-size:18px"> | ||
Thank You 🙏🙏 For Using our Website and Our Services. | ||
</div> | ||
</footer> | ||
</body> | ||
</html>""" | ||
pdf = HTML(string=html_content).write_pdf() | ||
def createHTMLVideoElement(videos: dict[str, str]) -> str: | ||
htmlstr = list() | ||
for vids in videos: | ||
htmlstr.append("<div style='margin-top:9px;'>") | ||
htmlstr.append(f"<span>{vids} ➡️ </span>") | ||
htmlstr.append(f"<a href='{videos[vids]}'>{videos[vids]}</a>") | ||
htmlstr.append("</div>") | ||
videosString = "".join(htmlstr) | ||
return videosString | ||
|
||
|
||
def recommentVideos(dosha: str) -> dict[str, str]: | ||
vids = df.loc[df["Doshas"] == dosha].drop("Doshas", axis=1).to_dict() | ||
return dict(zip(list(vids["Title"].values()), list(vids["Video"].values()))) | ||
|
||
|
||
def createPDF(consume: str, avoid: str, dosha: str, lifeStyle: str) -> bytes: | ||
with open(os.path.join("doc.html"), "r", encoding="utf8") as f: | ||
html_content = str(f.read()) | ||
videos = recommentVideos(dosha) | ||
html_content = html_content.format( | ||
dosha, createHTMLListString(consume, "<ul>", "</ul>"), createHTMLListString(avoid, "<ul>", "</ul>"), createHTMLListString(lifeStyle, "<ol>", "</ol>"), createHTMLVideoElement(videos=videos)) | ||
pdf = HTML(string=html_content).write_pdf(stylesheets=[ | ||
CSS(string="@page {size: A4; margin: 18mm 15mm 25mm 15mm;}")]) | ||
logger.info("Created PDF File") | ||
return pdf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters