Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,21 +3,22 @@ import json
|
|
| 3 |
import yaml
|
| 4 |
import httpx
|
| 5 |
from transformers import pipeline
|
| 6 |
-
import fitz # PyMuPDF
|
| 7 |
-
import docx # python-docx for
|
| 8 |
import logging
|
| 9 |
|
| 10 |
-
# Initialize the Hugging Face model pipeline (using text generation model for simplicity)
|
| 11 |
llm_model = pipeline('text-generation', model="bigscience/bloom-560m")
|
| 12 |
|
| 13 |
# Configure logging
|
| 14 |
logging.basicConfig(filename='api_client.log', level=logging.INFO,
|
| 15 |
format='%(asctime)s - %(levelname)s - %(message)s')
|
| 16 |
|
|
|
|
| 17 |
def parse_api_spec(api_spec_content):
|
| 18 |
"""
|
| 19 |
-
|
| 20 |
-
Assumes OpenAPI format.
|
| 21 |
"""
|
| 22 |
try:
|
| 23 |
# Try to load as JSON first
|
|
@@ -71,15 +72,36 @@ class APIClient:
|
|
| 71 |
"""
|
| 72 |
return interface_code
|
| 73 |
|
|
|
|
| 74 |
def use_llm_to_extract(api_spec):
|
| 75 |
"""
|
| 76 |
-
Uses a Hugging Face model to extract API details.
|
| 77 |
"""
|
| 78 |
-
prompt = f"
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
|
|
|
|
|
|
|
|
|
| 81 |
return response[0]['generated_text']
|
| 82 |
|
|
|
|
| 83 |
def read_pdf(file):
|
| 84 |
"""
|
| 85 |
Extracts text from PDF file using PyMuPDF (fitz).
|
|
@@ -90,6 +112,7 @@ def read_pdf(file):
|
|
| 90 |
text += page.get_text()
|
| 91 |
return text
|
| 92 |
|
|
|
|
| 93 |
def read_docx(file):
|
| 94 |
"""
|
| 95 |
Extracts text from DOC/DOCX file using python-docx.
|
|
@@ -98,6 +121,7 @@ def read_docx(file):
|
|
| 98 |
text = "\n".join([para.text for para in doc.paragraphs])
|
| 99 |
return text
|
| 100 |
|
|
|
|
| 101 |
def main():
|
| 102 |
st.title("API Spec Uploader and Python Interface Generator")
|
| 103 |
|
|
@@ -106,50 +130,47 @@ def main():
|
|
| 106 |
|
| 107 |
if uploaded_file is not None:
|
| 108 |
file_type = uploaded_file.type
|
| 109 |
-
|
| 110 |
-
# Handle different file types
|
| 111 |
if file_type == "application/json":
|
| 112 |
api_spec_content = uploaded_file.read().decode("utf-8")
|
| 113 |
elif file_type == "application/x-yaml":
|
| 114 |
api_spec_content = uploaded_file.read().decode("utf-8")
|
| 115 |
elif file_type == "application/pdf":
|
|
|
|
| 116 |
api_spec_content = read_pdf(uploaded_file)
|
| 117 |
elif file_type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document" or file_type == "application/octet-stream":
|
| 118 |
-
# Handle
|
| 119 |
api_spec_content = read_docx(uploaded_file)
|
| 120 |
else:
|
| 121 |
-
st.error(
|
| 122 |
return
|
| 123 |
|
| 124 |
-
# Display API Spec
|
| 125 |
-
st.subheader("Uploaded API Specification")
|
| 126 |
-
st.code(api_spec_content, language="json" if file_type == "application/json" else "yaml")
|
| 127 |
-
|
| 128 |
# Extract API endpoints and parameters using LLM
|
| 129 |
with st.spinner('Extracting API information using Hugging Face LLM...'):
|
| 130 |
extracted_info = use_llm_to_extract(api_spec_content)
|
| 131 |
st.subheader("Extracted Information from LLM")
|
| 132 |
st.write(extracted_info)
|
| 133 |
|
| 134 |
-
# Parse the API spec manually to display extracted endpoints
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
if endpoints:
|
| 138 |
-
st.subheader("Parsed Endpoints and Parameters")
|
| 139 |
-
for endpoint, params in endpoints.items():
|
| 140 |
-
st.write(f"**{endpoint}**")
|
| 141 |
-
st.json(params)
|
| 142 |
-
|
| 143 |
-
# Generate Python interface code with logging
|
| 144 |
-
python_interface_code = generate_python_interface(endpoints)
|
| 145 |
-
|
| 146 |
-
st.subheader("Generated Python Interface with Logging")
|
| 147 |
-
st.code(python_interface_code, language="python")
|
| 148 |
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
|
| 154 |
if __name__ == '__main__':
|
| 155 |
main()
|
|
|
|
| 3 |
import yaml
|
| 4 |
import httpx
|
| 5 |
from transformers import pipeline
|
| 6 |
+
import fitz # PyMuPDF for PDF
|
| 7 |
+
import docx # python-docx for DOCX
|
| 8 |
import logging
|
| 9 |
|
| 10 |
+
# Initialize the Hugging Face model pipeline (using a text generation model for simplicity)
|
| 11 |
llm_model = pipeline('text-generation', model="bigscience/bloom-560m")
|
| 12 |
|
| 13 |
# Configure logging
|
| 14 |
logging.basicConfig(filename='api_client.log', level=logging.INFO,
|
| 15 |
format='%(asctime)s - %(levelname)s - %(message)s')
|
| 16 |
|
| 17 |
+
|
| 18 |
def parse_api_spec(api_spec_content):
|
| 19 |
"""
|
| 20 |
+
Parses the uploaded API specification and returns endpoints and their parameters.
|
| 21 |
+
Assumes OpenAPI format (JSON/YAML).
|
| 22 |
"""
|
| 23 |
try:
|
| 24 |
# Try to load as JSON first
|
|
|
|
| 72 |
"""
|
| 73 |
return interface_code
|
| 74 |
|
| 75 |
+
|
| 76 |
def use_llm_to_extract(api_spec):
|
| 77 |
"""
|
| 78 |
+
Uses a Hugging Face model to extract all API methods, parameters, and response details from the given API spec.
|
| 79 |
"""
|
| 80 |
+
prompt = f"""
|
| 81 |
+
Please extract all the API methods, their respective endpoints, parameters (both required and optional), and response details from the following API specification:
|
| 82 |
+
|
| 83 |
+
{api_spec}
|
| 84 |
+
|
| 85 |
+
The extracted details should include:
|
| 86 |
+
1. HTTP Method (e.g., GET, POST, PUT, DELETE)
|
| 87 |
+
2. API Endpoint (e.g., /api/v1/recharge-cash-account)
|
| 88 |
+
3. Input Parameters:
|
| 89 |
+
- Parameter name
|
| 90 |
+
- Parameter type (e.g., String, Integer)
|
| 91 |
+
- Parameter description (if provided)
|
| 92 |
+
- Whether the parameter is mandatory (M) or optional (O)
|
| 93 |
+
4. Output (Response) Parameters:
|
| 94 |
+
- Parameter name
|
| 95 |
+
- Parameter type (e.g., String, Integer)
|
| 96 |
+
- Parameter description (if provided)
|
| 97 |
+
5. Example Request and Example Response
|
| 98 |
|
| 99 |
+
Provide a clear summary of each API method and its details.
|
| 100 |
+
"""
|
| 101 |
+
response = llm_model(prompt, max_length=500, num_return_sequences=1)
|
| 102 |
return response[0]['generated_text']
|
| 103 |
|
| 104 |
+
|
| 105 |
def read_pdf(file):
|
| 106 |
"""
|
| 107 |
Extracts text from PDF file using PyMuPDF (fitz).
|
|
|
|
| 112 |
text += page.get_text()
|
| 113 |
return text
|
| 114 |
|
| 115 |
+
|
| 116 |
def read_docx(file):
|
| 117 |
"""
|
| 118 |
Extracts text from DOC/DOCX file using python-docx.
|
|
|
|
| 121 |
text = "\n".join([para.text for para in doc.paragraphs])
|
| 122 |
return text
|
| 123 |
|
| 124 |
+
|
| 125 |
def main():
|
| 126 |
st.title("API Spec Uploader and Python Interface Generator")
|
| 127 |
|
|
|
|
| 130 |
|
| 131 |
if uploaded_file is not None:
|
| 132 |
file_type = uploaded_file.type
|
| 133 |
+
|
|
|
|
| 134 |
if file_type == "application/json":
|
| 135 |
api_spec_content = uploaded_file.read().decode("utf-8")
|
| 136 |
elif file_type == "application/x-yaml":
|
| 137 |
api_spec_content = uploaded_file.read().decode("utf-8")
|
| 138 |
elif file_type == "application/pdf":
|
| 139 |
+
# Extract text from PDF
|
| 140 |
api_spec_content = read_pdf(uploaded_file)
|
| 141 |
elif file_type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document" or file_type == "application/octet-stream":
|
| 142 |
+
# Handle DOCX or unknown types
|
| 143 |
api_spec_content = read_docx(uploaded_file)
|
| 144 |
else:
|
| 145 |
+
st.error("Unsupported file format.")
|
| 146 |
return
|
| 147 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
# Extract API endpoints and parameters using LLM
|
| 149 |
with st.spinner('Extracting API information using Hugging Face LLM...'):
|
| 150 |
extracted_info = use_llm_to_extract(api_spec_content)
|
| 151 |
st.subheader("Extracted Information from LLM")
|
| 152 |
st.write(extracted_info)
|
| 153 |
|
| 154 |
+
# Parse the API spec manually to display extracted endpoints (if JSON or YAML)
|
| 155 |
+
if file_type in ["application/json", "application/x-yaml"]:
|
| 156 |
+
endpoints = parse_api_spec(api_spec_content)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
|
| 158 |
+
if endpoints:
|
| 159 |
+
st.subheader("Parsed Endpoints and Parameters")
|
| 160 |
+
for endpoint, params in endpoints.items():
|
| 161 |
+
st.write(f"**{endpoint}**")
|
| 162 |
+
st.json(params)
|
| 163 |
+
|
| 164 |
+
# Generate Python interface code with logging
|
| 165 |
+
python_interface_code = generate_python_interface(endpoints)
|
| 166 |
+
|
| 167 |
+
st.subheader("Generated Python Interface with Logging")
|
| 168 |
+
st.code(python_interface_code, language="python")
|
| 169 |
+
|
| 170 |
+
st.download_button(label="Download Python Interface",
|
| 171 |
+
data=python_interface_code,
|
| 172 |
+
file_name="api_interface.py",
|
| 173 |
+
mime="text/plain")
|
| 174 |
|
| 175 |
if __name__ == '__main__':
|
| 176 |
main()
|