import streamlit as st import json import yaml import httpx from transformers import pipeline import fitz # PyMuPDF for PDF import docx # python-docx for DOCX import logging # Initialize the Hugging Face model pipeline (using a text generation model for simplicity) llm_model = pipeline('text-generation', model="bigscience/bloom-560m") # Configure logging logging.basicConfig(filename='api_client.log', level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') def parse_api_spec(api_spec_content): """ Parses the uploaded API specification and returns endpoints and their parameters. Assumes OpenAPI format (JSON/YAML). """ try: # Try to load as JSON first api_spec = json.loads(api_spec_content) except json.JSONDecodeError: # If it's not JSON, try loading as YAML api_spec = yaml.safe_load(api_spec_content) # Extract paths and methods endpoints = {} if 'paths' in api_spec: for path, methods in api_spec['paths'].items(): for method, details in methods.items(): # Extract parameters params = details.get('parameters', []) param_info = {param['name']: param['in'] for param in params} endpoints[f"{method.upper()} {path}"] = param_info return endpoints def generate_python_interface(endpoints): """ Generates a Python interface based on the extracted API endpoints and parameters. Includes logging of all API requests and responses. """ interface_code = """ import httpx import logging # Configure logging logging.basicConfig(filename='api_calls.log', level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') class APIClient: def __init__(self, base_url): self.base_url = base_url self.client = httpx.Client() def call_api(self, method, endpoint, params=None, json=None): url = f"{self.base_url}{endpoint}" logging.info(f"Requesting {method} {url} with params: {params} and body: {json}") response = self.client.request(method, url, params=params, json=json) logging.info(f"Received response: {response.status_code} - {response.text}") return response.json() # Example Usage: # api_client = APIClient(base_url="https://api.example.com") # response = api_client.call_api("GET", "/some-endpoint", {"param1": "value1"}) """ return interface_code def extract_sections_from_docx(file): """ Extract key sections from DOCX file, focusing on API information. """ doc = docx.Document(file) api_details = [] # Extract paragraphs and tables for para in doc.paragraphs: if 'Resource URL:' in para.text or 'HTTP Method:' in para.text: api_details.append(para.text) # Extract tables (for parameters) for table in doc.tables: table_data = [] for row in table.rows: row_data = [cell.text for cell in row.cells] table_data.append(row_data) api_details.append(table_data) return api_details def use_llm_to_extract(api_spec): """ Uses an LLM to extract API methods, parameters, and response details from the given API spec. """ prompt = f""" Please extract all the API methods, their respective endpoints, parameters (both required and optional), and response details from the following API specification: {api_spec} The extracted details should include: 1. HTTP Method (e.g., GET, POST) 2. API Endpoint URL 3. Input Parameters: - Parameter name, type, description, and if it's required or optional 4. Response Parameters: - Parameter name, type, description 5. Example Request and Example Response if available. """ response = llm_model(prompt, max_length=1000, num_return_sequences=1) return response[0]['generated_text'] def read_pdf(file): """ Extracts text from PDF file using PyMuPDF (fitz). """ doc = fitz.open(stream=file.read(), filetype="pdf") text = "" for page in doc: text += page.get_text() return text def read_docx(file): """ Extracts text from DOC/DOCX file using python-docx. """ doc = docx.Document(file) text = "\n".join([para.text for para in doc.paragraphs]) return text def main(): st.title("API Spec Uploader and Python Interface Generator") # Upload API Spec File uploaded_file = st.file_uploader("Upload API Spec (JSON, YAML, PDF, DOC)", type=["json", "yaml", "pdf", "docx"]) if uploaded_file is not None: file_type = uploaded_file.type if file_type == "application/json": api_spec_content = uploaded_file.read().decode("utf-8") elif file_type == "application/x-yaml": api_spec_content = uploaded_file.read().decode("utf-8") elif file_type == "application/pdf": # Extract text from PDF api_spec_content = read_pdf(uploaded_file) elif file_type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document" or file_type == "application/octet-stream": # Handle DOCX or unknown types api_spec_content = read_docx(uploaded_file) else: st.error("Unsupported file format.") return # Extract API endpoints and parameters using LLM with st.spinner('Extracting API information using Hugging Face LLM...'): extracted_info = use_llm_to_extract(api_spec_content) st.subheader("Extracted Information from LLM") st.write(extracted_info) # Parse the API spec manually to display extracted endpoints (if JSON or YAML) if file_type in ["application/json", "application/x-yaml"]: endpoints = parse_api_spec(api_spec_content) if endpoints: st.subheader("Parsed Endpoints and Parameters") for endpoint, params in endpoints.items(): st.write(f"**{endpoint}**") st.json(params) # Generate Python interface code with logging python_interface_code = generate_python_interface(endpoints) st.subheader("Generated Python Interface with Logging") st.code(python_interface_code, language="python") st.download_button(label="Download Python Interface", data=python_interface_code, file_name="api_interface.py", mime="text/plain") if __name__ == '__main__': main()