File size: 2,288 Bytes
8700a34
52a7d50
d99db10
6bbd3ca
bda7361
c39e604
 
af17670
 
e36dd54
70678a5
b3ae078
4556b98
1fab2aa
76bbd8a
d99db10
 
fbe5c6d
0cd7858
6a96e5e
17a7267
ab46adf
 
836458e
86a0b7a
6bbd3ca
bda7361
 
 
 
 
 
 
 
 
 
2ad530f
31d9e37
574f9e3
 
bda7361
574f9e3
 
 
 
 
6bbd3ca
574f9e3
6bbd3ca
2ad530f
 
 
 
 
 
 
 
 
 
 
 
 
 
8601b67
36dea63
 
 
 
 
 
 
 
 
1
2
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import fitz
import io
from fastapi import FastAPI, File, UploadFile, Form, HTTPException
from fastapi.responses import JSONResponse
from transformers import pipeline
from PIL import Image
from io import BytesIO
from starlette.middleware import Middleware
from starlette.middleware.cors import CORSMiddleware
from pdf2image import convert_from_bytes
from pydub import AudioSegment
import numpy as np
import json
import torchaudio
import torch
from pydub import AudioSegment
import speech_recognition as sr
import logging
import asyncio
from concurrent.futures import ThreadPoolExecutor
import re
from pydantic import BaseModel
from typing import List, Dict, Any

app = FastAPI()

# Set up CORS middleware
origins = ["*"]  # or specify your list of allowed origins
app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

code_generation_model = pipeline('text-generation', model='codeparrot/codeparrot-small')

description = """
## Image-based Document QA
This API performs document question answering using a LayoutLMv2-based model.

### Endpoints:
- **POST /uploadfile/:** Upload an image file to extract text and answer provided questions.
- **POST /pdfQA/:** Provide a PDF file to extract text and answer provided questions.
"""

app = FastAPI(docs_url="/", description=description)

@app.post("/generate_code/", description="Generate code based on the provided prompt.")
async def generate_code(prompt: str = Form(...)):
    try:
        # Use the code generation model to generate code based on the provided prompt
        generated_code = code_generation_model(prompt, max_length=200, num_return_sequences=1)

        # Extract the generated code from the model's output
        generated_code_text = generated_code[0]['generated_text']

        # Return the generated code as a response
        return {"generated_code": generated_code_text}

    except Exception as e:
        return JSONResponse(content=f"Error generating code: {str(e)}", status_code=500)

# Set up CORS middleware
origins = ["*"]  # or specify your list of allowed origins
app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)