Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,14 +7,12 @@ import json
|
|
| 7 |
import google.generativeai as genai
|
| 8 |
|
| 9 |
# --- IMPORTANT: Configure your API Key ---
|
| 10 |
-
# For local testing, you can set this directly.
|
| 11 |
-
# For Hugging Face Spaces, you will set this in "Secrets".
|
| 12 |
-
# os.environ['GOOGLE_API_KEY'] = "YOUR_API_KEY_HERE"
|
| 13 |
try:
|
| 14 |
genai.configure(api_key=os.environ.get("GOOGLE_API_KEY"))
|
| 15 |
except AttributeError:
|
| 16 |
print("WARNING: GOOGLE_API_KEY secret not set. LLM functionality will fail.")
|
| 17 |
|
|
|
|
| 18 |
app = Flask(__name__)
|
| 19 |
|
| 20 |
# The HTML template remains the same
|
|
@@ -81,13 +79,12 @@ def generate_readme_with_llm(repo_path):
|
|
| 81 |
"""
|
| 82 |
Analyzes the repo and uses an LLM to generate the README content.
|
| 83 |
"""
|
| 84 |
-
# 1. Gather context: file structure and content of key files
|
| 85 |
file_structure = ""
|
| 86 |
file_contents = ""
|
| 87 |
-
file_limit = 5
|
| 88 |
-
|
|
|
|
| 89 |
for root, _, files in os.walk(repo_path):
|
| 90 |
-
# Ignore git directory
|
| 91 |
if '.git' in root:
|
| 92 |
continue
|
| 93 |
|
|
@@ -98,51 +95,37 @@ def generate_readme_with_llm(repo_path):
|
|
| 98 |
sub_indent = ' ' * 4 * (level + 1)
|
| 99 |
for f in files[:file_limit]:
|
| 100 |
file_structure += f"{sub_indent}{f}\n"
|
| 101 |
-
# Read content of a few key files
|
| 102 |
try:
|
| 103 |
with open(os.path.join(root, f), 'r', errors='ignore') as file:
|
| 104 |
-
content = file.read(
|
| 105 |
file_contents += f"\n--- Start of {f} ---\n{content}\n--- End of {f} ---\n"
|
| 106 |
except Exception:
|
| 107 |
-
continue
|
| 108 |
|
| 109 |
-
# 2. Create a detailed prompt for the LLM
|
| 110 |
prompt = f"""
|
| 111 |
You are an expert technical writer tasked with creating a high-quality README.md for a GitHub repository.
|
| 112 |
Analyze the following repository context and generate a comprehensive and user-friendly README.
|
| 113 |
|
| 114 |
**Repository Context:**
|
| 115 |
-
|
| 116 |
**1. File Structure:**
|
| 117 |
```
|
| 118 |
{file_structure}
|
| 119 |
```
|
| 120 |
-
|
| 121 |
**2. Content of Key Files:**
|
| 122 |
```
|
| 123 |
{file_contents}
|
| 124 |
```
|
| 125 |
-
|
| 126 |
**Instructions:**
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
4. **Usage:** A simple example of how to run the application.
|
| 132 |
-
|
| 133 |
-
**Rules:**
|
| 134 |
-
- The output must be in valid Markdown format.
|
| 135 |
-
- Be professional and clear.
|
| 136 |
-
- If you cannot determine a piece of information (like a specific run command), suggest a common default and state that it might need to be verified.
|
| 137 |
-
- Do not invent features that are not evident from the provided context.
|
| 138 |
"""
|
| 139 |
|
| 140 |
-
# 3. Call the LLM
|
| 141 |
print("Sending request to Gemini API...")
|
| 142 |
model = genai.GenerativeModel('gemini-1.5-flash-latest')
|
| 143 |
response = model.generate_content(prompt)
|
| 144 |
|
| 145 |
-
# Clean up the response - sometimes the model wraps it in ```markdown
|
| 146 |
readme_text = response.text.strip()
|
| 147 |
if readme_text.startswith("```markdown"):
|
| 148 |
readme_text = readme_text[10:]
|
|
@@ -157,31 +140,42 @@ def index():
|
|
| 157 |
|
| 158 |
@app.route('/generate', methods=['POST'])
|
| 159 |
def generate():
|
| 160 |
-
|
| 161 |
-
if not
|
| 162 |
-
return jsonify({"error": "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
|
| 164 |
if not os.environ.get("GOOGLE_API_KEY"):
|
| 165 |
return jsonify({"error": "Server is missing the GOOGLE_API_KEY. Cannot contact the LLM."}), 500
|
| 166 |
|
| 167 |
temp_dir = tempfile.mkdtemp()
|
| 168 |
try:
|
|
|
|
| 169 |
git.Repo.clone_from(repo_url, temp_dir)
|
|
|
|
| 170 |
|
| 171 |
-
|
| 172 |
-
if not cloned_folders:
|
| 173 |
-
raise Exception("Cloning seems to have failed, the directory is empty.")
|
| 174 |
-
|
| 175 |
-
repo_root_path = os.path.join(temp_dir, cloned_folders[0])
|
| 176 |
-
|
| 177 |
-
# --- NEW: Call the LLM-powered function ---
|
| 178 |
-
readme_content = generate_readme_with_llm(repo_root_path)
|
| 179 |
|
| 180 |
return jsonify({"readme": readme_content})
|
| 181 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
except Exception as e:
|
| 183 |
-
|
|
|
|
| 184 |
finally:
|
|
|
|
| 185 |
shutil.rmtree(temp_dir)
|
| 186 |
|
| 187 |
if __name__ == '__main__':
|
|
|
|
| 7 |
import google.generativeai as genai
|
| 8 |
|
| 9 |
# --- IMPORTANT: Configure your API Key ---
|
|
|
|
|
|
|
|
|
|
| 10 |
try:
|
| 11 |
genai.configure(api_key=os.environ.get("GOOGLE_API_KEY"))
|
| 12 |
except AttributeError:
|
| 13 |
print("WARNING: GOOGLE_API_KEY secret not set. LLM functionality will fail.")
|
| 14 |
|
| 15 |
+
# Create the Flask app instance
|
| 16 |
app = Flask(__name__)
|
| 17 |
|
| 18 |
# The HTML template remains the same
|
|
|
|
| 79 |
"""
|
| 80 |
Analyzes the repo and uses an LLM to generate the README content.
|
| 81 |
"""
|
|
|
|
| 82 |
file_structure = ""
|
| 83 |
file_contents = ""
|
| 84 |
+
file_limit = 5
|
| 85 |
+
char_limit_per_file = 2000
|
| 86 |
+
|
| 87 |
for root, _, files in os.walk(repo_path):
|
|
|
|
| 88 |
if '.git' in root:
|
| 89 |
continue
|
| 90 |
|
|
|
|
| 95 |
sub_indent = ' ' * 4 * (level + 1)
|
| 96 |
for f in files[:file_limit]:
|
| 97 |
file_structure += f"{sub_indent}{f}\n"
|
|
|
|
| 98 |
try:
|
| 99 |
with open(os.path.join(root, f), 'r', errors='ignore') as file:
|
| 100 |
+
content = file.read(char_limit_per_file)
|
| 101 |
file_contents += f"\n--- Start of {f} ---\n{content}\n--- End of {f} ---\n"
|
| 102 |
except Exception:
|
| 103 |
+
continue
|
| 104 |
|
|
|
|
| 105 |
prompt = f"""
|
| 106 |
You are an expert technical writer tasked with creating a high-quality README.md for a GitHub repository.
|
| 107 |
Analyze the following repository context and generate a comprehensive and user-friendly README.
|
| 108 |
|
| 109 |
**Repository Context:**
|
|
|
|
| 110 |
**1. File Structure:**
|
| 111 |
```
|
| 112 |
{file_structure}
|
| 113 |
```
|
|
|
|
| 114 |
**2. Content of Key Files:**
|
| 115 |
```
|
| 116 |
{file_contents}
|
| 117 |
```
|
|
|
|
| 118 |
**Instructions:**
|
| 119 |
+
Generate a README.md with these sections: Project Title, About the Project, Getting Started, and Usage.
|
| 120 |
+
- Infer the project purpose, technologies, and setup commands from the files.
|
| 121 |
+
- The output must be valid Markdown.
|
| 122 |
+
- If a command is unknown, suggest a common default (e.g., `npm install`).
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
"""
|
| 124 |
|
|
|
|
| 125 |
print("Sending request to Gemini API...")
|
| 126 |
model = genai.GenerativeModel('gemini-1.5-flash-latest')
|
| 127 |
response = model.generate_content(prompt)
|
| 128 |
|
|
|
|
| 129 |
readme_text = response.text.strip()
|
| 130 |
if readme_text.startswith("```markdown"):
|
| 131 |
readme_text = readme_text[10:]
|
|
|
|
| 140 |
|
| 141 |
@app.route('/generate', methods=['POST'])
|
| 142 |
def generate():
|
| 143 |
+
data = request.get_json()
|
| 144 |
+
if not data or 'url' not in data:
|
| 145 |
+
return jsonify({"error": "Request body must be JSON with a 'url' key."}), 400
|
| 146 |
+
|
| 147 |
+
repo_url = data.get('url')
|
| 148 |
+
|
| 149 |
+
# --- BUG FIX: Make URL validation more flexible ---
|
| 150 |
+
if not repo_url or "github.com/" not in repo_url.lower():
|
| 151 |
+
print(f"Validation failed for URL: {repo_url}")
|
| 152 |
+
return jsonify({"error": "A valid public GitHub repository URL is required."}), 400
|
| 153 |
|
| 154 |
if not os.environ.get("GOOGLE_API_KEY"):
|
| 155 |
return jsonify({"error": "Server is missing the GOOGLE_API_KEY. Cannot contact the LLM."}), 500
|
| 156 |
|
| 157 |
temp_dir = tempfile.mkdtemp()
|
| 158 |
try:
|
| 159 |
+
print(f"Cloning repository: {repo_url} into {temp_dir}")
|
| 160 |
git.Repo.clone_from(repo_url, temp_dir)
|
| 161 |
+
print("Cloning successful.")
|
| 162 |
|
| 163 |
+
readme_content = generate_readme_with_llm(temp_dir)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
|
| 165 |
return jsonify({"readme": readme_content})
|
| 166 |
|
| 167 |
+
except git.exc.GitCommandError as e:
|
| 168 |
+
error_message = str(e).lower()
|
| 169 |
+
print(f"Git error: {error_message}")
|
| 170 |
+
if "authentication failed" in error_message or "not found" in error_message:
|
| 171 |
+
return jsonify({"error": "Failed to clone. Please ensure the URL is correct and the repository is public."}), 400
|
| 172 |
+
else:
|
| 173 |
+
return jsonify({"error": f"A Git error occurred during cloning."}), 500
|
| 174 |
except Exception as e:
|
| 175 |
+
print(f"An unexpected error occurred: {e}")
|
| 176 |
+
return jsonify({"error": f"An unexpected error occurred on the server."}), 500
|
| 177 |
finally:
|
| 178 |
+
print(f"Cleaning up temporary directory: {temp_dir}")
|
| 179 |
shutil.rmtree(temp_dir)
|
| 180 |
|
| 181 |
if __name__ == '__main__':
|