Spaces:
Runtime error
Runtime error
Upload folder using huggingface_hub
Browse files- .gradio/certificate.pem +31 -0
- README.md +2 -8
- book_generator.py +89 -0
.gradio/certificate.pem
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
-----BEGIN CERTIFICATE-----
|
| 2 |
+
MIIFazCCA1OgAwIBAgIRAIIQz7DSQONZRGPgu2OCiwAwDQYJKoZIhvcNAQELBQAw
|
| 3 |
+
TzELMAkGA1UEBhMCVVMxKTAnBgNVBAoTIEludGVybmV0IFNlY3VyaXR5IFJlc2Vh
|
| 4 |
+
cmNoIEdyb3VwMRUwEwYDVQQDEwxJU1JHIFJvb3QgWDEwHhcNMTUwNjA0MTEwNDM4
|
| 5 |
+
WhcNMzUwNjA0MTEwNDM4WjBPMQswCQYDVQQGEwJVUzEpMCcGA1UEChMgSW50ZXJu
|
| 6 |
+
ZXQgU2VjdXJpdHkgUmVzZWFyY2ggR3JvdXAxFTATBgNVBAMTDElTUkcgUm9vdCBY
|
| 7 |
+
MTCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoCggIBAK3oJHP0FDfzm54rVygc
|
| 8 |
+
h77ct984kIxuPOZXoHj3dcKi/vVqbvYATyjb3miGbESTtrFj/RQSa78f0uoxmyF+
|
| 9 |
+
0TM8ukj13Xnfs7j/EvEhmkvBioZxaUpmZmyPfjxwv60pIgbz5MDmgK7iS4+3mX6U
|
| 10 |
+
A5/TR5d8mUgjU+g4rk8Kb4Mu0UlXjIB0ttov0DiNewNwIRt18jA8+o+u3dpjq+sW
|
| 11 |
+
T8KOEUt+zwvo/7V3LvSye0rgTBIlDHCNA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td7u+Nxe5AW0wdeRlN8NwdC
|
| 26 |
+
jNPElpzVmbUq4JUagEiuTDkHzsxHpFKVK7q4+63SM1N95R1NbdWhscdCb+ZAJzVc
|
| 27 |
+
oyi3B43njTOQ5yOf+1CceWxG1bQVs5ZufpsMljq4Ui0/1lvh+wjChP4kqKOJ2qxq
|
| 28 |
+
4RgqsahDYVvTH9w7jXbyLeiNdd8XM2w9U/t7y0Ff/9yi0GE44Za4rF2LN9d11TPA
|
| 29 |
+
mRGunUHBcnWEvgJBQl9nJEiU0Zsnvgc/ubhPgXRR4Xq37Z0j4r7g1SgEEzwxA57d
|
| 30 |
+
emyPxgcYxn/eR44/KJ4EBs+lVDR3veyJm+kXQ99b21/+jh5Xos1AnX5iItreGCc=
|
| 31 |
+
-----END CERTIFICATE-----
|
README.md
CHANGED
|
@@ -1,12 +1,6 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
|
| 4 |
-
colorFrom: purple
|
| 5 |
-
colorTo: indigo
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 5.9.0
|
| 8 |
-
app_file: app.py
|
| 9 |
-
pinned: false
|
| 10 |
---
|
| 11 |
-
|
| 12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
---
|
| 2 |
+
title: iLLAi-book
|
| 3 |
+
app_file: book_generator.py
|
|
|
|
|
|
|
| 4 |
sdk: gradio
|
| 5 |
sdk_version: 5.9.0
|
|
|
|
|
|
|
| 6 |
---
|
|
|
|
|
|
book_generator.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from langchain_groq import ChatGroq
|
| 3 |
+
from langchain.prompts import ChatPromptTemplate
|
| 4 |
+
from langchain_core.output_parsers import StrOutputParser
|
| 5 |
+
from langchain_core.runnables import RunnablePassthrough
|
| 6 |
+
from typing import Dict
|
| 7 |
+
import gradio as gr # Import Gradio
|
| 8 |
+
|
| 9 |
+
# Step 3: Set the environment variable for the Groq API Key
|
| 10 |
+
os.environ["GROQ_API_KEY"] = "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" # Updated API Key
|
| 11 |
+
|
| 12 |
+
# Step 4: Define helper functions for structured book generation
|
| 13 |
+
def create_book_agent(
|
| 14 |
+
model_name: str = "llama3-70b-8192",
|
| 15 |
+
temperature: float = 0.7,
|
| 16 |
+
max_tokens: int = 8192,
|
| 17 |
+
**kwargs
|
| 18 |
+
) -> ChatGroq:
|
| 19 |
+
"""Create a LangChain agent for book writing."""
|
| 20 |
+
prompt_template = ChatPromptTemplate.from_messages([
|
| 21 |
+
("system", "You are a creative writer. Write high-quality, engaging books for any genre."),
|
| 22 |
+
("human", "{input}")
|
| 23 |
+
])
|
| 24 |
+
llm = ChatGroq(model=model_name, temperature=temperature, max_tokens=max_tokens, **kwargs)
|
| 25 |
+
chain = prompt_template | llm | StrOutputParser()
|
| 26 |
+
return chain
|
| 27 |
+
|
| 28 |
+
def generate_chapter(title: str, synopsis: str, agent) -> str:
|
| 29 |
+
"""Generate a full chapter given a title and synopsis."""
|
| 30 |
+
query = f"Write a detailed chapter based on the following synopsis:\n\nTitle: {title}\n\nSynopsis: {synopsis}"
|
| 31 |
+
try:
|
| 32 |
+
return agent.invoke({"input": query})
|
| 33 |
+
except Exception as e:
|
| 34 |
+
print(f"An error occurred while generating the chapter: {e}")
|
| 35 |
+
return ""
|
| 36 |
+
|
| 37 |
+
def write_book(agent, title: str, outline: Dict[str, str]) -> str:
|
| 38 |
+
"""
|
| 39 |
+
Generate a complete book.
|
| 40 |
+
|
| 41 |
+
Args:
|
| 42 |
+
agent: The LangChain agent for generating text.
|
| 43 |
+
title (str): The title of the book.
|
| 44 |
+
outline (Dict[str, str]): A dictionary with chapter titles as keys and synopses as values.
|
| 45 |
+
|
| 46 |
+
Returns:
|
| 47 |
+
str: The full book as a single string.
|
| 48 |
+
"""
|
| 49 |
+
book = f"# {title}\n\n"
|
| 50 |
+
for chapter_title, chapter_synopsis in outline.items():
|
| 51 |
+
book += f"## {chapter_title}\n\n"
|
| 52 |
+
chapter_text = generate_chapter(chapter_title, chapter_synopsis, agent)
|
| 53 |
+
book += chapter_text + "\n\n"
|
| 54 |
+
return book
|
| 55 |
+
|
| 56 |
+
# Step 5: Create the agent
|
| 57 |
+
book_agent = create_book_agent()
|
| 58 |
+
|
| 59 |
+
# Step 6: Gradio interface
|
| 60 |
+
def gradio_interface():
|
| 61 |
+
"""Create a Gradio interface for book generation."""
|
| 62 |
+
with gr.Blocks() as demo:
|
| 63 |
+
gr.Markdown("## Book Generator")
|
| 64 |
+
book_title = gr.Textbox(label="Book Title")
|
| 65 |
+
book_outline = gr.Textbox(label="Book Outline (Structured format, e.g., 'Chapter 1: Synopsis 1; Chapter 2: Synopsis 2')")
|
| 66 |
+
generate_button = gr.Button("Generate Book")
|
| 67 |
+
output = gr.Textbox(label="Generated Book", interactive=False)
|
| 68 |
+
|
| 69 |
+
def generate_book_interface(title, outline):
|
| 70 |
+
try:
|
| 71 |
+
# Normalize the outline input
|
| 72 |
+
outline_dict = {}
|
| 73 |
+
chapters = outline.split(';') # Split by semicolon for each chapter
|
| 74 |
+
for chapter in chapters:
|
| 75 |
+
if ':' in chapter:
|
| 76 |
+
title, synopsis = chapter.split(':', 1)
|
| 77 |
+
outline_dict[title.strip()] = synopsis.strip()
|
| 78 |
+
|
| 79 |
+
print(f"Processed Outline: {outline_dict}") # Debug statement
|
| 80 |
+
return write_book(book_agent, title, outline_dict)
|
| 81 |
+
except Exception as e:
|
| 82 |
+
return f"An error occurred: {e}"
|
| 83 |
+
|
| 84 |
+
generate_button.click(generate_book_interface, inputs=[book_title, book_outline], outputs=output)
|
| 85 |
+
|
| 86 |
+
demo.launch(share=True)
|
| 87 |
+
|
| 88 |
+
if __name__ == "__main__":
|
| 89 |
+
gradio_interface()
|