Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,8 @@ import json
|
|
| 3 |
import yaml
|
| 4 |
import httpx
|
| 5 |
from transformers import pipeline
|
|
|
|
|
|
|
| 6 |
import logging
|
| 7 |
|
| 8 |
# Initialize the Hugging Face model pipeline (using text generation model for simplicity)
|
|
@@ -12,7 +14,6 @@ llm_model = pipeline('text-generation', model="bigscience/bloom-560m")
|
|
| 12 |
logging.basicConfig(filename='api_client.log', level=logging.INFO,
|
| 13 |
format='%(asctime)s - %(levelname)s - %(message)s')
|
| 14 |
|
| 15 |
-
|
| 16 |
def parse_api_spec(api_spec_content):
|
| 17 |
"""
|
| 18 |
This function parses the uploaded API specification and returns endpoints and their parameters.
|
|
@@ -79,18 +80,50 @@ def use_llm_to_extract(api_spec):
|
|
| 79 |
|
| 80 |
return response[0]['generated_text']
|
| 81 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
def main():
|
| 83 |
st.title("API Spec Uploader and Python Interface Generator")
|
| 84 |
|
| 85 |
# Upload API Spec File
|
| 86 |
-
uploaded_file = st.file_uploader("Upload API Spec (JSON
|
| 87 |
|
| 88 |
if uploaded_file is not None:
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
# Display API Spec
|
| 92 |
st.subheader("Uploaded API Specification")
|
| 93 |
-
st.code(api_spec_content, language="json" if
|
| 94 |
|
| 95 |
# Extract API endpoints and parameters using LLM
|
| 96 |
with st.spinner('Extracting API information using Hugging Face LLM...'):
|
|
|
|
| 3 |
import yaml
|
| 4 |
import httpx
|
| 5 |
from transformers import pipeline
|
| 6 |
+
import fitz # PyMuPDF
|
| 7 |
+
import docx # python-docx for handling doc/docx files
|
| 8 |
import logging
|
| 9 |
|
| 10 |
# Initialize the Hugging Face model pipeline (using text generation model for simplicity)
|
|
|
|
| 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 |
This function parses the uploaded API specification and returns endpoints and their parameters.
|
|
|
|
| 80 |
|
| 81 |
return response[0]['generated_text']
|
| 82 |
|
| 83 |
+
def read_pdf(file):
|
| 84 |
+
"""
|
| 85 |
+
Extracts text from PDF file using PyMuPDF (fitz).
|
| 86 |
+
"""
|
| 87 |
+
doc = fitz.open(stream=file.read(), filetype="pdf")
|
| 88 |
+
text = ""
|
| 89 |
+
for page in doc:
|
| 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.
|
| 96 |
+
"""
|
| 97 |
+
doc = docx.Document(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 |
|
| 104 |
# Upload API Spec File
|
| 105 |
+
uploaded_file = st.file_uploader("Upload API Spec (JSON, YAML, PDF, DOC)", type=["json", "yaml", "pdf", "docx"])
|
| 106 |
|
| 107 |
if uploaded_file is not None:
|
| 108 |
+
file_type = uploaded_file.type
|
| 109 |
+
|
| 110 |
+
if file_type == "application/json":
|
| 111 |
+
api_spec_content = uploaded_file.read().decode("utf-8")
|
| 112 |
+
elif file_type == "application/x-yaml":
|
| 113 |
+
api_spec_content = uploaded_file.read().decode("utf-8")
|
| 114 |
+
elif file_type == "application/pdf":
|
| 115 |
+
# Extract text from PDF
|
| 116 |
+
api_spec_content = read_pdf(uploaded_file)
|
| 117 |
+
elif file_type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
|
| 118 |
+
# Extract text from DOCX
|
| 119 |
+
api_spec_content = read_docx(uploaded_file)
|
| 120 |
+
else:
|
| 121 |
+
st.error("Unsupported file format.")
|
| 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...'):
|