Spaces:
Sleeping
Sleeping
Neopy
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,113 @@
|
|
| 1 |
-
from docs import generate_docs
|
| 2 |
import gradio as gr
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
)
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
)
|
| 28 |
-
gr.Markdown("<div style='text-align: center;'>Made with ❤ by <a href='https://huggingface.co/theNeofr'>NeoPy</a></div>")
|
| 29 |
-
|
| 30 |
-
# When the button is clicked, call the generate_docs function.
|
| 31 |
-
generate_button.click(fn=generate_docs, inputs=repo_id_input, outputs=output_box)
|
| 32 |
|
| 33 |
-
|
| 34 |
-
demo.launch(
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
from urllib.parse import urlparse
|
| 4 |
|
| 5 |
+
try:
|
| 6 |
+
from groq import Groq
|
| 7 |
+
except ImportError:
|
| 8 |
+
print("Error: The Groq Python client is required. Install with 'pip install groq'")
|
| 9 |
|
| 10 |
+
def extract_repo_id(repo_input):
|
| 11 |
+
if repo_input.startswith(("http://", "https://")):
|
| 12 |
+
parsed = urlparse(repo_input)
|
| 13 |
+
path = parsed.path.strip("/")
|
| 14 |
+
parts = path.split("/")
|
| 15 |
+
if len(parts) >= 2:
|
| 16 |
+
return f"{parts[0]}/{parts[1]}"
|
| 17 |
+
return None
|
| 18 |
+
else:
|
| 19 |
+
if "/" in repo_input:
|
| 20 |
+
return repo_input
|
| 21 |
+
return None
|
| 22 |
+
|
| 23 |
+
def get_repo_info(repo_id):
|
| 24 |
+
try:
|
| 25 |
+
# Try model endpoint
|
| 26 |
+
model_response = requests.get(f"https://huggingface.co/api/models/{repo_id}")
|
| 27 |
+
if model_response.status_code == 200:
|
| 28 |
+
return model_response.json(), "model"
|
| 29 |
+
|
| 30 |
+
# Try dataset endpoint
|
| 31 |
+
dataset_response = requests.get(f"https://huggingface.co/api/datasets/{repo_id}")
|
| 32 |
+
if dataset_response.status_code == 200:
|
| 33 |
+
return dataset_response.json(), "dataset"
|
| 34 |
+
|
| 35 |
+
return None, None
|
| 36 |
+
except Exception as e:
|
| 37 |
+
print(f"Error fetching repo info: {e}")
|
| 38 |
+
return None, None
|
| 39 |
+
|
| 40 |
+
def generate_readme(repo_input, api_key):
|
| 41 |
+
try:
|
| 42 |
+
# Validate and extract repo ID
|
| 43 |
+
repo_id = extract_repo_id(repo_input)
|
| 44 |
+
if not repo_id:
|
| 45 |
+
return "Invalid repository format. Please use 'user/repo' or a Hugging Face URL"
|
| 46 |
+
|
| 47 |
+
# Get repository information
|
| 48 |
+
repo_info, repo_type = get_repo_info(repo_id)
|
| 49 |
+
if not repo_info:
|
| 50 |
+
return "Repository not found or inaccessible"
|
| 51 |
+
|
| 52 |
+
# Prepare prompt for Groq
|
| 53 |
+
prompt = f"""Generate a professional README.md for the Hugging Face {repo_type} repository {repo_id}.
|
| 54 |
+
Use the following information:
|
| 55 |
+
- Author: {repo_info.get('author', 'Unknown')}
|
| 56 |
+
- Description: {repo_info.get('description', 'No description')}
|
| 57 |
+
- Tags: {', '.join(repo_info.get('tags', []))}
|
| 58 |
+
- License: {repo_info.get('license', 'Unknown')}
|
| 59 |
+
|
| 60 |
+
Include these sections:
|
| 61 |
+
1. Overview
|
| 62 |
+
2. Installation
|
| 63 |
+
3. Usage
|
| 64 |
+
4. Examples
|
| 65 |
+
5. License
|
| 66 |
+
6. Citation (if applicable)
|
| 67 |
+
|
| 68 |
+
Format the README in proper Markdown with code blocks where appropriate."""
|
| 69 |
+
|
| 70 |
+
# Initialize Groq client
|
| 71 |
+
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 72 |
+
|
| 73 |
+
# Generate completion
|
| 74 |
+
completion = client.chat.completions.create(
|
| 75 |
+
messages=[{"role": "user", "content": prompt}],
|
| 76 |
+
model="mixtral-8x7b-32768",
|
| 77 |
+
temperature=0.3,
|
| 78 |
+
max_tokens=1024
|
| 79 |
)
|
| 80 |
+
|
| 81 |
+
return completion.choices[0].message.content
|
| 82 |
+
|
| 83 |
+
except Exception as e:
|
| 84 |
+
return f"Error generating README: {str(e)}"
|
| 85 |
|
| 86 |
+
with gr.Blocks(title="HF Repo README Generator") as demo:
|
| 87 |
+
gr.Markdown("# 🚀 Hugging Face Repository README Generator")
|
| 88 |
+
|
| 89 |
+
with gr.Row():
|
| 90 |
+
with gr.Column():
|
| 91 |
+
repo_input = gr.Textbox(
|
| 92 |
+
label="Hugging Face Repository URL or ID",
|
| 93 |
+
placeholder="Enter 'user/repo' or full URL...",
|
| 94 |
+
max_lines=1
|
| 95 |
+
)
|
| 96 |
+
api_key = gr.Textbox(
|
| 97 |
+
label="Groq API Key",
|
| 98 |
+
type="password",
|
| 99 |
+
placeholder="Enter your Groq API key..."
|
| 100 |
+
)
|
| 101 |
+
submit_btn = gr.Button("Generate README", variant="primary")
|
| 102 |
+
|
| 103 |
+
with gr.Row():
|
| 104 |
+
output = gr.Markdown(label="Generated README")
|
| 105 |
+
|
| 106 |
+
submit_btn.click(
|
| 107 |
+
fn=generate_readme,
|
| 108 |
+
inputs=[repo_input, api_key],
|
| 109 |
+
outputs=output
|
| 110 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
+
if __name__ == "__main__":
|
| 113 |
+
demo.launch()
|