import os
import requests
import json
from io import BytesIO

from flask import Flask, jsonify, render_template, request, send_file, Response

app = Flask(__name__)


@app.route("/")
def index():
    # Basic HTML string
    html = '''
    <!DOCTYPE html>
    <html>
    <head>
    <title>Hugging Face Docs /llms.txt</title>
    </head>
    <body>
    
    <a href="https://llmstxt.org/">/llms.txt</a> files for <a href="https://huggingface.co/docs">Hugging Face Docs</a>
    
    <ul>
      <li><a href="/hub/llms.txt">/hub/llms.txt</a></li>
      <li><a href="/transformers/llms.txt">/transformers/llms.txt</a></li>
      <li><a href="/diffusers/llms.txt">/diffusers/llms.txt</a></li>
      <li><a href="/accelerate/llms.txt">/accelerate/llms.txt</a></li>
      <li><a href="/huggingface_hub/llms.txt">/huggingface_hub/llms.txt</a></li>
    </ul>  
    
    
    </body>
    </html>
    '''
    return html

@app.route('/<library>/llms.txt')
def llm_text(library):
    # Replace this URL with your actual file URL
    remote_url = f"https://huggingface.co/mishig/llms-txt/raw/main/{library}.txt"
    
    try:
        # Fetch the remote file
        response = requests.get(remote_url)
        response.raise_for_status()  # Raise an exception for bad status codes
        
        # Return the content as a text file
        return Response(
            response.text,
            mimetype='text/plain',
            headers={
                'Content-Disposition': 'inline',
                'Cache-Control': 'no-cache'
            }
        )
    except requests.RequestException as e:
        return f"Error fetching file: {str(e)}", 500

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=7860)