Spaces:
Running
Running
Upload folder using huggingface_hub
Browse files- .gradio/certificate.pem +31 -0
- Procfile +1 -0
- README.md +20 -7
- book_generator.py +94 -0
- requirements.txt +3 -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/7V3LvSye0rgTBIlDHCNAymg4VMk7BPZ7hm/ELNKjD+Jo2FR3qyH
|
12 |
+
B5T0Y3HsLuJvW5iB4YlcNHlsdu87k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|
+
oyi3B43njTOQ5yOf+1CceWxG1bQVs5ZufpsMljq4Ui0/1lvh+wjChP4kqKOJ2qxq
|
28 |
+
4RgqsahDYVvTH9w7jXbyLeiNdd8XM2w9U/t7y0Ff/9yi0GE44Za4rF2LN9d11TPA
|
29 |
+
mRGunUHBcnWEvgJBQl9nJEiU0Zsnvgc/ubhPgXRR4Xq37Z0j4r7g1SgEEzwxA57d
|
30 |
+
emyPxgcYxn/eR44/KJ4EBs+lVDR3veyJm+kXQ99b21/+jh5Xos1AnX5iItreGCc=
|
31 |
+
-----END CERTIFICATE-----
|
Procfile
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
web: python book_generator.py
|
README.md
CHANGED
@@ -1,12 +1,25 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
|
4 |
-
colorFrom: yellow
|
5 |
-
colorTo: blue
|
6 |
sdk: gradio
|
7 |
sdk_version: 5.9.0
|
8 |
-
app_file: app.py
|
9 |
-
pinned: false
|
10 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
-
|
|
|
|
1 |
---
|
2 |
+
title: iLL-Ai-Book_writer
|
3 |
+
app_file: book_generator.py
|
|
|
|
|
4 |
sdk: gradio
|
5 |
sdk_version: 5.9.0
|
|
|
|
|
6 |
---
|
7 |
+
title: iLLAi-book
|
8 |
+
app_file: book_generator.py
|
9 |
+
sdk: gradio
|
10 |
+
sdk_version: 5.9.0
|
11 |
+
---
|
12 |
+
=======
|
13 |
+
---
|
14 |
+
title: iLLAi-book
|
15 |
+
app_file: book_generator.py
|
16 |
+
sdk: gradio
|
17 |
+
sdk_version: 5.9.0
|
18 |
+
---
|
19 |
+
|
20 |
+
# Environment Variables
|
21 |
+
|
22 |
+
Before running the application, make sure to set the following environment variables:
|
23 |
|
24 |
+
- `GROQ_API_KEY`: Your Groq API key.
|
25 |
+
- `MODEL_NAME`: The name of the model to be used (optional, defaults to "llama-3.1-8b-instant").
|
book_generator.py
ADDED
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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"] = os.getenv("GROQ_API_KEY") # Use environment variable for API Key
|
11 |
+
|
12 |
+
# Step 4: Define helper functions for structured book generation
|
13 |
+
def create_book_agent(
|
14 |
+
model_name: str = os.getenv("MODEL_NAME", "llama-3.1-8b-instant"), # Use environment variable for model name
|
15 |
+
temperature: float = 0.7,
|
16 |
+
max_tokens: int = 16384, # Increased token limit
|
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 |
+
token = os.getenv("HUGGINGFACE_TOKEN") # Get Hugging Face token from environment variable
|
25 |
+
llm = ChatGroq(model=model_name, temperature=temperature, max_tokens=max_tokens, token=token, **kwargs)
|
26 |
+
chain = prompt_template | llm | StrOutputParser()
|
27 |
+
return chain
|
28 |
+
|
29 |
+
def generate_chapter(title: str, synopsis: str, agent) -> str:
|
30 |
+
"""Generate a full chapter given a title and synopsis."""
|
31 |
+
query = f"Write a detailed chapter based on the following synopsis:\n\nTitle: {title}\n\nSynopsis: {synopsis}"
|
32 |
+
try:
|
33 |
+
return agent.invoke({"input": query})
|
34 |
+
except Exception as e:
|
35 |
+
print(f"An error occurred while generating the chapter: {e}")
|
36 |
+
return ""
|
37 |
+
|
38 |
+
def write_book(agent, title: str, outline: Dict[str, str]) -> str:
|
39 |
+
"""
|
40 |
+
Generate a complete book.
|
41 |
+
|
42 |
+
Args:
|
43 |
+
agent: The LangChain agent for generating text.
|
44 |
+
title (str): The title of the book.
|
45 |
+
outline (Dict[str, str]): A dictionary with chapter titles as keys and synopses as values.
|
46 |
+
|
47 |
+
Returns:
|
48 |
+
str: The full book as a single string.
|
49 |
+
"""
|
50 |
+
book = f"# {title}\n\n"
|
51 |
+
for chapter_title, chapter_synopsis in outline.items():
|
52 |
+
book += f"## {chapter_title}\n\n"
|
53 |
+
chapter_text = generate_chapter(chapter_title, chapter_synopsis, agent)
|
54 |
+
book += chapter_text + "\n\n"
|
55 |
+
return book
|
56 |
+
|
57 |
+
# Step 5: Create the agent
|
58 |
+
book_agent = create_book_agent()
|
59 |
+
|
60 |
+
# Step 6: Gradio interface
|
61 |
+
def gradio_interface(api_key: str = ""):
|
62 |
+
"""Create a Gradio interface for book generation."""
|
63 |
+
with gr.Blocks() as demo:
|
64 |
+
gr.Markdown("## Book Generator")
|
65 |
+
gr.Markdown("This application was created by iLL-Ai AaronAllton and a team of Groq agents that write books.") # Updated note
|
66 |
+
book_title = gr.Textbox(label="Book Title")
|
67 |
+
book_outline = gr.Textbox(label="Book Outline (Structured format, e.g., 'Chapter 1: Synopsis 1; Chapter 2: Synopsis 2')") # Updated prompt
|
68 |
+
generate_button = gr.Button("Generate Book")
|
69 |
+
output = gr.Textbox(label="Generated Book", interactive=False)
|
70 |
+
|
71 |
+
def generate_book_interface(title, outline):
|
72 |
+
try:
|
73 |
+
# Normalize the outline input
|
74 |
+
outline_dict = {}
|
75 |
+
chapters = outline.split(';') # Split by semicolon for each chapter
|
76 |
+
for chapter in chapters:
|
77 |
+
if ':' in chapter:
|
78 |
+
title, synopsis = chapter.split(':', 1)
|
79 |
+
outline_dict[title.strip()] = synopsis.strip()
|
80 |
+
else:
|
81 |
+
# Handle cases where the input might not follow the expected format
|
82 |
+
outline_dict[chapter.strip()] = "No synopsis provided."
|
83 |
+
|
84 |
+
print(f"Processed Outline: {outline_dict}") # Debug statement
|
85 |
+
return write_book(book_agent, title, outline_dict)
|
86 |
+
except Exception as e:
|
87 |
+
return f"An error occurred: {e}"
|
88 |
+
|
89 |
+
generate_button.click(generate_book_interface, inputs=[book_title, book_outline], outputs=output)
|
90 |
+
|
91 |
+
demo.launch(share=True) # Enable sharing of the Gradio app
|
92 |
+
|
93 |
+
if __name__ == "__main__":
|
94 |
+
gradio_interface()
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
langchain
|
3 |
+
langchain_groq
|