From 0d676f55f777ed0045ed7fddecfb4ea585b2480a Mon Sep 17 00:00:00 2001 From: sharathdoes Date: Tue, 18 Nov 2025 16:22:28 +0530 Subject: [PATCH 1/2] llm --- .gitignore | 1 - server.py | 36 ++++- templates/reader.html | 325 +++++++++++++++++++++++++++++++----------- 3 files changed, 274 insertions(+), 88 deletions(-) diff --git a/.gitignore b/.gitignore index 9e1d25d..32f227b 100644 --- a/.gitignore +++ b/.gitignore @@ -8,7 +8,6 @@ wheels/ # Virtual environments .venv - # Custom *_data/ *.epub diff --git a/server.py b/server.py index 9c870dc..019c493 100644 --- a/server.py +++ b/server.py @@ -2,17 +2,51 @@ import pickle from functools import lru_cache from typing import Optional - +from groq import Groq +from pydantic import BaseModel +import dotenv +dotenv.load_dotenv() from fastapi import FastAPI, Request, HTTPException from fastapi.responses import HTMLResponse, FileResponse from fastapi.staticfiles import StaticFiles from fastapi.templating import Jinja2Templates +from fastapi.responses import StreamingResponse + from reader3 import Book, BookMetadata, ChapterContent, TOCEntry app = FastAPI() templates = Jinja2Templates(directory="templates") +class ChatRequest(BaseModel): + message: str + +client = Groq(api_key=os.getenv("GROQ_KEY")) + +@app.post("/chat-stream") +async def chat_stream(req: ChatRequest): + def generate(): + stream = client.chat.completions.create( + model="llama-3.1-8b-instant", + stream=True, + messages=[{"role": "user", "content": req.message}], + ) + + for chunk in stream: + if chunk.choices and chunk.choices[0].delta: + delta = chunk.choices[0].delta.content + + if delta: + # Groq may return list or string + if isinstance(delta, list): + for item in delta: + if "text" in item: + yield item["text"] + else: + yield delta + + return StreamingResponse(generate(), media_type="text/plain") + # Where are the book folders located? BOOKS_DIR = "." diff --git a/templates/reader.html b/templates/reader.html index c012edc..12dd153 100644 --- a/templates/reader.html +++ b/templates/reader.html @@ -4,40 +4,178 @@ {{ book.metadata.title }} + + @@ -45,56 +183,29 @@ ← Back to Library - {% macro render_toc(items) %} - {% endmacro %} {{ render_toc(book.toc) }} - +
@@ -103,27 +214,80 @@
{% if prev_idx is not none %} - ← Previous + ← Previous {% else %} - ← Previous + ← Previous {% endif %} - - Section {{ chapter_index + 1 }} of {{ book.spine|length }} - + Section {{ chapter_index + 1 }} of {{ book.spine|length }} {% if next_idx is not none %} - Next → + Next → {% else %} - Next → + Next → {% endif %}
+ +
+
Chat with Groq
+ +
+ +
+ + +
+
+ + From 45807c8ef7dd165f5193b4daa5ad2f1bc80802a0 Mon Sep 17 00:00:00 2001 From: sharathdoes Date: Tue, 18 Nov 2025 16:34:34 +0530 Subject: [PATCH 2/2] html changes --- templates/reader.html | 200 +++++++++++++++++++----------------------- 1 file changed, 89 insertions(+), 111 deletions(-) diff --git a/templates/reader.html b/templates/reader.html index 12dd153..69e7968 100644 --- a/templates/reader.html +++ b/templates/reader.html @@ -4,98 +4,38 @@ {{ book.metadata.title }} - - @@ -183,29 +122,56 @@ ← Back to Library + {% macro render_toc(items) %} -
    +
      {% for item in items %} -
    • - {% set is_active = current_chapter.href == item.file_href %} - - - {{ item.title }} - - - {% if item.children %} - {{ render_toc(item.children) }} - {% endif %} -
    • +
    • + + {% set is_active = current_chapter.href == item.file_href %} + + + + {{ item.title }} + + + {% if item.children %} + {{ render_toc(item.children) }} + {% endif %} +
    • {% endfor %} -
    +
{% endmacro %} {{ render_toc(book.toc) }}
- +
@@ -214,23 +180,23 @@
{% if prev_idx is not none %} - ← Previous + ← Previous {% else %} - ← Previous + ← Previous {% endif %} - Section {{ chapter_index + 1 }} of {{ book.spine|length }} + + Section {{ chapter_index + 1 }} of {{ book.spine|length }} + {% if next_idx is not none %} - Next → + Next → {% else %} - Next → + Next → {% endif %}
- -
Chat with Groq
@@ -243,6 +209,7 @@
- - + \ No newline at end of file