Brettapps commited on
Commit
c91d2bc
·
verified ·
1 Parent(s): fd3a694

Upload folder using huggingface_hub

Browse files
.gemini/extensions/huggingface/GEMINI.md ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Hugging Face Extension for Gemini CLI
2
+
3
+ This extension provides tools to interact with the Hugging Face Hub and access the EbookBuilder knowledge base.
4
+
5
+ ## Available Tools
6
+
7
+ - `search_repos`: Search for models, datasets, or spaces on the Hub.
8
+ - `get_repo_info`: Get detailed information about a repository.
9
+ - `download_repo`: Download a repository to a local directory.
10
+ - `query_knowledge_base`: Search the EbookBuilder knowledge base for documentation and guides.
11
+
12
+ ## Usage Guidelines
13
+
14
+ - Always use the `HUGGINGFACE_HUB_TOKEN` environment variable for authenticated requests.
15
+ - When searching, specify the `repo_type` (model, dataset, or space).
16
+ - The knowledge base contains specific workflows for ebook generation. Query it if you need guidance on the ebook building process.
.gemini/extensions/huggingface/KNOWLEDGE_BASE.md ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # EbookBuilder Knowledge Base
2
+
3
+ ## Overview
4
+ EbookBuilder is a system designed to leverage AI models on Hugging Face to generate, format, and publish ebooks.
5
+
6
+ ## Key Components
7
+
8
+ ### 1. Content Generation
9
+ - **Models**: Use Large Language Models (LLMs) like Llama-3, Mistral, or Gemini (via HF) to generate book content.
10
+ - **Prompting**: Use structured prompts to ensure consistent style and tone across chapters.
11
+
12
+ ### 2. Formatting & Conversion
13
+ - **Markdown**: The primary format for draft content.
14
+ - **Conversion Tools**: Use libraries like `pandoc` or custom Python scripts to convert Markdown to EPUB, PDF, or MOBI.
15
+ - **Images**: Generate covers and illustrations using Diffusion models (e.g., Stable Diffusion) hosted on Hugging Face.
16
+
17
+ ### 3. Hosting & Deployment
18
+ - **Hugging Face Spaces**: Host the EbookBuilder interface (using Gradio or Streamlit) for user interaction.
19
+ - **Datasets**: Store generated book metadata and files in Hugging Face Datasets for versioning and sharing.
20
+
21
+ ## Workflows
22
+
23
+ ### Creating a New Ebook
24
+ 1. Define the book title and outline.
25
+ 2. Generate content chapter by chapter using a chosen LLM.
26
+ 3. Review and edit the markdown content.
27
+ 4. Generate cover art using an image model.
28
+ 5. Compile the final ebook using the conversion module.
29
+
30
+ ## Best Practices
31
+ - **Human-in-the-loop**: Always review AI-generated content for accuracy and flow.
32
+ - **Modular Design**: Keep the generation logic separate from the formatting logic for easier updates.
33
+ - **Token Management**: Monitor API usage when using hosted models to avoid hitting rate limits.
.gemini/extensions/huggingface/gemini-extension.json ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "huggingface",
3
+ "version": "0.1.0",
4
+ "mcpServers": {
5
+ "HuggingFaceMcpServer": {
6
+ "command": "${extensionPath}/run.sh",
7
+ "args": []
8
+ }
9
+ }
10
+ }
.gemini/extensions/huggingface/pyproject.toml ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [project]
2
+ name = "huggingface"
3
+ version = "0.1.0"
4
+ description = "Hugging Face MCP server extension"
5
+ dependencies = [
6
+ "huggingface_hub",
7
+ "mcp[server]",
8
+ "fastmcp",
9
+ "python-dotenv",
10
+ "absl-py"
11
+ ]
12
+
13
+ [build-system]
14
+ requires = ["setuptools>=61.0"]
15
+ build-backend = "setuptools.build_meta"
.gemini/extensions/huggingface/run.sh ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/bash
2
+ set -e
3
+
4
+ # Change to the directory containing this script.
5
+ cd "$(dirname "${BASH_SOURCE[0]}")" || { echo "ERROR: Could not change to script directory." >&2; exit 1; }
6
+
7
+ # Check for uv and install if not present
8
+ if ! command -v uv &> /dev/null
9
+ then
10
+ echo "'uv' is not found. Attempting to install it..."
11
+ curl -LsSf https://astral.sh/uv/install.sh | sh
12
+ export PATH="$HOME/.local/bin:$PATH"
13
+ fi
14
+
15
+ # Create virtual environment
16
+ if [ ! -d ".venv" ]; then
17
+ echo "Creating virtual environment..."
18
+ uv venv
19
+ fi
20
+
21
+ # Install dependencies
22
+ echo "Installing dependencies..."
23
+ uv pip install .
24
+
25
+ # Set the token from the user's provided value if it's in the environment
26
+ # The agent will handle the token securely.
27
+
28
+ # Start the server
29
+ echo "Starting Hugging Face server..."
30
+ ./.venv/bin/python3 -m huggingface.server
.gemini/extensions/huggingface/src/huggingface/__init__.py ADDED
File without changes
.gemini/extensions/huggingface/src/huggingface/__pycache__/__init__.cpython-312.pyc ADDED
Binary file (184 Bytes). View file
 
.gemini/extensions/huggingface/src/huggingface/__pycache__/server.cpython-312.pyc ADDED
Binary file (1.08 kB). View file
 
.gemini/extensions/huggingface/src/huggingface/server.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sys
2
+ from absl import app
3
+ from mcp.server import fastmcp
4
+ from .tools import HuggingFaceTools
5
+
6
+ def main(argv):
7
+ hf_tools = HuggingFaceTools()
8
+ mcp = fastmcp.FastMCP("HuggingFaceMcpServer")
9
+
10
+ mcp.add_tool(hf_tools.meta_orchestrator)
11
+ mcp.add_tool(hf_tools.search_repos)
12
+ mcp.add_tool(hf_tools.get_repo_info)
13
+ mcp.add_tool(hf_tools.download_repo)
14
+ mcp.add_tool(hf_tools.query_knowledge_base)
15
+
16
+ # New Agent Prototypes
17
+ mcp.add_tool(hf_tools.fact_check_text)
18
+ mcp.add_tool(hf_tools.generate_illustration_prompt)
19
+ mcp.add_tool(hf_tools.synthesize_audiobook_chapter)
20
+ mcp.add_tool(hf_tools.generate_marketing_package)
21
+ mcp.add_tool(hf_tools.audit_project_costs)
22
+ mcp.add_tool(hf_tools.request_human_review)
23
+
24
+ # Advanced Agent Prototypes
25
+ mcp.add_tool(hf_tools.localize_content)
26
+ mcp.add_tool(hf_tools.legal_copyright_scan)
27
+ mcp.add_tool(hf_tools.create_character_companion)
28
+ mcp.add_tool(hf_tools.analyze_reader_sentiment)
29
+ mcp.add_tool(hf_tools.optimize_accessibility)
30
+ mcp.add_tool(hf_tools.build_continuity_graph)
31
+ mcp.add_tool(hf_tools.perform_market_research)
32
+
33
+ mcp.run()
34
+
35
+ if __name__ == "__main__":
36
+ app.run(main)
.gemini/extensions/huggingface/src/huggingface/tools.py ADDED
@@ -0,0 +1,156 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+ from huggingface_hub import HfApi, hf_hub_download, snapshot_download, InferenceClient
4
+
5
+ class HuggingFaceTools:
6
+ def __init__(self, token=None):
7
+ self.token = token or os.environ.get("HUGGINGFACE_HUB_TOKEN") or os.environ.get("HUGGINGFACE_ACCESS_KEY")
8
+ self.api = HfApi(token=self.token)
9
+ self.client = InferenceClient(token=self.token)
10
+
11
+ def search_repos(self, query: str, repo_type: str = "model", limit: int = 5):
12
+ """Search for repositories on the Hugging Face Hub."""
13
+ if repo_type == "model":
14
+ repos = self.api.list_models(search=query, limit=limit)
15
+ elif repo_type == "dataset":
16
+ repos = self.api.list_datasets(search=query, limit=limit)
17
+ elif repo_type == "space":
18
+ repos = self.api.list_spaces(search=query, limit=limit)
19
+ else:
20
+ return f"Invalid repo_type: {repo_type}"
21
+
22
+ return [{"id": r.id, "author": r.author, "lastModified": r.lastModified} for r in repos]
23
+
24
+ def get_repo_info(self, repo_id: str, repo_type: str = "model"):
25
+ """Get detailed information about a repository."""
26
+ if repo_type == "model":
27
+ return self.api.model_info(repo_id)
28
+ elif repo_type == "dataset":
29
+ return self.api.dataset_info(repo_id)
30
+ elif repo_type == "space":
31
+ return self.api.space_info(repo_id)
32
+ return "Invalid repo_type"
33
+
34
+ def download_repo(self, repo_id: str, repo_type: str = "model", local_dir: str = None):
35
+ """Download a whole repository."""
36
+ return snapshot_download(repo_id=repo_id, repo_type=repo_type, local_dir=local_dir)
37
+
38
+ def query_knowledge_base(self, query: str):
39
+ """Query the EbookBuilder knowledge base for information."""
40
+ kb_path = os.path.join(os.path.dirname(__file__), "../../KNOWLEDGE_BASE.md")
41
+ if not os.path.exists(kb_path):
42
+ return "Knowledge base not found."
43
+
44
+ with open(kb_path, "r") as f:
45
+ content = f.read()
46
+ if query.lower() in content.lower():
47
+ return f"Found information in KB: {content[:1000]}..."
48
+ return "No matching information found in knowledge base."
49
+
50
+ # --- Upgraded Agents ---
51
+
52
+ def meta_orchestrator(self, task: str):
53
+ """The Meta Agent: Analyzes a complex request and maps it to specific agent tools."""
54
+ prompt = f"Given the task: '{task}', identify the sequence of tools needed from the following list: [search_repos, get_repo_info, fact_check_text, generate_illustration_prompt, synthesize_audiobook_chapter, generate_marketing_package, localize_content, legal_copyright_scan]. Return a JSON plan."
55
+ # Use InferenceClient to act as a planner
56
+ response = self.client.text_generation(prompt, model="meta-llama/Meta-Llama-3-8B-Instruct", max_new_tokens=200)
57
+ return {"task": task, "suggested_plan": response}
58
+
59
+ def fact_check_text(self, text: str):
60
+ """Research Agent: Validates claims using LLM-based verification."""
61
+ prompt = f"Fact check the following text and identify any potential inaccuracies: '{text}'"
62
+ response = self.client.text_generation(prompt, model="meta-llama/Meta-Llama-3-8B-Instruct", max_new_tokens=300)
63
+ return {"status": "Complete", "analysis": response}
64
+
65
+ def generate_illustration_prompt(self, chapter_text: str):
66
+ """Illustrator Agent: Refines a narrative description into a high-quality Diffusion prompt."""
67
+ prompt = f"Convert this text into a detailed prompt for an image generator (like Stable Diffusion): '{chapter_text[:500]}'"
68
+ response = self.client.text_generation(prompt, model="meta-llama/Meta-Llama-3-8B-Instruct", max_new_tokens=150)
69
+ return {"image_prompt": response}
70
+
71
+ def synthesize_audiobook_chapter(self, text: str, voice_id: str = "default"):
72
+ """Audiobook Agent: Prepares audio synthesis parameters."""
73
+ # Note: Real TTS often requires binary output handling; this prepares the request metadata
74
+ return {
75
+ "status": "Ready",
76
+ "voice_profile": voice_id,
77
+ "text_preview": text[:100] + "...",
78
+ "recommended_model": "facebook/parler-tts-mini-v1"
79
+ }
80
+
81
+ def generate_marketing_package(self, title: str, summary: str):
82
+ """Marketing Agent: Generates copy for multiple platforms."""
83
+ prompt = f"Create an Amazon blurb and 3 social media posts for a book titled '{title}' with this summary: '{summary}'"
84
+ response = self.client.text_generation(prompt, model="meta-llama/Meta-Llama-3-8B-Instruct", max_new_tokens=400)
85
+ return {"marketing_materials": response}
86
+
87
+ def audit_project_costs(self, tokens_used: int):
88
+ """Auditor Agent: Financial tracking."""
89
+ return {
90
+ "tokens_total": tokens_used,
91
+ "estimated_spend_usd": tokens_used * 0.000002,
92
+ "currency": "USD",
93
+ "status": "Audit Complete"
94
+ }
95
+
96
+ def request_human_review(self, content_id: str, content_preview: str):
97
+ """Review Agent: Checkpoint."""
98
+ return {
99
+ "review_endpoint": f"https://huggingface.co/spaces/Brettapps/EbookBuilder/review",
100
+ "content_id": content_id,
101
+ "status": "WAITING_FOR_USER"
102
+ }
103
+
104
+ def localize_content(self, text: str, target_lang: str):
105
+ """Localization Agent: Real-time translation."""
106
+ prompt = f"Translate the following text to {target_lang}: '{text}'"
107
+ response = self.client.text_generation(prompt, model="meta-llama/Meta-Llama-3-8B-Instruct", max_new_tokens=500)
108
+ return {"target_lang": target_lang, "translation": response}
109
+
110
+ def legal_copyright_scan(self, text: str):
111
+ """Legal Guard: Risk analysis."""
112
+ prompt = f"Analyze this text for potential copyright or trademark risks: '{text[:1000]}'"
113
+ response = self.client.text_generation(prompt, model="meta-llama/Meta-Llama-3-8B-Instruct", max_new_tokens=200)
114
+ return {"legal_report": response}
115
+
116
+ def create_character_companion(self, character_bio: str):
117
+ """Companion Agent: Chatbot system prompt generation."""
118
+ prompt = f"Create a system instruction for an AI to act as this character: '{character_bio}'"
119
+ response = self.client.text_generation(prompt, model="meta-llama/Meta-Llama-3-8B-Instruct", max_new_tokens=300)
120
+ return {"system_instruction": response}
121
+
122
+ def analyze_reader_sentiment(self, feedback: str):
123
+ """Analyst Agent: Sentiment analysis."""
124
+ result = self.client.text_classification(feedback, model="distilbert-base-uncased-finetuned-sst-2-english")
125
+ return {"sentiment_analysis": result}
126
+
127
+ def optimize_accessibility(self, text: str):
128
+ """Accessibility Agent: Content simplification and Alt-text."""
129
+ prompt = f"Summarize this text for high accessibility and suggest Alt-text for any implied visuals: '{text[:500]}'"
130
+ response = self.client.text_generation(prompt, model="meta-llama/Meta-Llama-3-8B-Instruct", max_new_tokens=200)
131
+ return {"accessibility_report": response}
132
+
133
+ def build_continuity_graph(self, manuscript: str):
134
+ """Continuity Agent: Entity extraction."""
135
+ prompt = f"Extract all main characters and their locations from this text: '{manuscript[:1000]}'"
136
+ response = self.client.text_generation(prompt, model="meta-llama/Meta-Llama-3-8B-Instruct", max_new_tokens=300)
137
+ return {"world_entities": response}
138
+
139
+ def perform_market_research(self, niche: str):
140
+ """Market Research Agent: Analyzes market trends, competitors, and audience for a specific book niche."""
141
+ prompt = f"""
142
+ Act as a Senior Market Research Analyst. Perform a deep dive into the ebook market for the niche: '{niche}'.
143
+ 1. Identify the top 3 trending sub-genres or topics in this niche.
144
+ 2. Analyze the 'typical' successful competitor (price point, cover style, length).
145
+ 3. Define the core target audience (demographics, pain points, what they are looking for).
146
+ 4. Suggest 3 high-traffic keywords for Amazon/Google.
147
+
148
+ Provide a structured report.
149
+ """
150
+ response = self.client.text_generation(prompt, model="meta-llama/Meta-Llama-3-8B-Instruct", max_new_tokens=800)
151
+ return {
152
+ "niche": niche,
153
+ "research_report": response,
154
+ "status": "Success",
155
+ "source": "Hugging Face Inference (Llama-3)"
156
+ }