Spaces:
Sleeping
Sleeping
ShobhitKori
commited on
Commit
·
dfe177d
1
Parent(s):
7e7a80f
Add application file
Browse files- Dockerfile +18 -0
- codet5_model.py +57 -0
- ffmpeg_test.py +8 -0
- main.py +41 -0
- requirements.txt +0 -0
- utils.py +12 -0
- whisper_model.py +12 -0
Dockerfile
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Use Python image
|
| 2 |
+
FROM python:3.11-slim
|
| 3 |
+
|
| 4 |
+
# Set workdir
|
| 5 |
+
WORKDIR /app
|
| 6 |
+
|
| 7 |
+
# Install dependencies
|
| 8 |
+
COPY requirements.txt .
|
| 9 |
+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
| 10 |
+
|
| 11 |
+
# Copy app files
|
| 12 |
+
COPY . .
|
| 13 |
+
|
| 14 |
+
# Expose the port Hugging Face expects
|
| 15 |
+
EXPOSE 7860
|
| 16 |
+
|
| 17 |
+
# Command to run FastAPI
|
| 18 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
codet5_model.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# from transformers import pipeline
|
| 2 |
+
|
| 3 |
+
# #load Codet5 model
|
| 4 |
+
# code_pipeline = pipeline("text-generation", model="Salesforce/codet5-base")
|
| 5 |
+
|
| 6 |
+
# def generate_code(instruction: str) -> str:
|
| 7 |
+
# result = code_pipeline(instruction, max_length=256, clean_up_tokenization_spaces=True)
|
| 8 |
+
# return result[0]["generated_text"]
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
# from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 12 |
+
|
| 13 |
+
# tokenizer = AutoTokenizer.from_pretrained("./models", trust_remote_code=True)
|
| 14 |
+
# model = AutoModelForSeq2SeqLM.from_pretrained("./models", trust_remote_code=True)
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
# def generate_code(instruction: str) -> str:
|
| 18 |
+
# prompt = f"Generate Python code for the following instruction: {instruction}"
|
| 19 |
+
|
| 20 |
+
# inputs = tokenizer(prompt, return_tensors="pt", padding=True, truncation=True)
|
| 21 |
+
# output = model.generate(**inputs, max_length=256)
|
| 22 |
+
# generated_code = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 23 |
+
|
| 24 |
+
# return generated_code
|
| 25 |
+
|
| 26 |
+
# from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 27 |
+
|
| 28 |
+
# tokenizer = AutoTokenizer.from_pretrained("./models")
|
| 29 |
+
# model = AutoModelForSeq2SeqLM.from_pretrained("./models")
|
| 30 |
+
|
| 31 |
+
# def generate_code(instruction: str) -> str:
|
| 32 |
+
# prompt = f"Generate Python code: {instruction}"
|
| 33 |
+
# inputs = tokenizer(prompt, return_tensors="pt", padding=True, truncation=True)
|
| 34 |
+
# output = model.generate(**inputs, max_length=256)
|
| 35 |
+
# return tokenizer.decode(output[0], skip_special_tokens=True)
|
| 36 |
+
|
| 37 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 38 |
+
|
| 39 |
+
# Global variables but not initialized
|
| 40 |
+
tokenizer = None
|
| 41 |
+
model = None
|
| 42 |
+
|
| 43 |
+
def load_model():
|
| 44 |
+
global tokenizer, model
|
| 45 |
+
if tokenizer is None or model is None:
|
| 46 |
+
print("Loading CodeT5 model...")
|
| 47 |
+
tokenizer = AutoTokenizer.from_pretrained("Salesforce/codet5p-770m-py")
|
| 48 |
+
model = AutoModelForSeq2SeqLM.from_pretrained("Salesforce/codet5p-770m-py")
|
| 49 |
+
print("Model loaded.")
|
| 50 |
+
|
| 51 |
+
def generate_code(instruction: str) -> str:
|
| 52 |
+
load_model() # Ensure model is loaded before use
|
| 53 |
+
prompt = f"# Instruction: {instruction}\n# Python Code:\n"
|
| 54 |
+
|
| 55 |
+
inputs = tokenizer(prompt, return_tensors="pt", padding=True, truncation=True)
|
| 56 |
+
output = model.generate(**inputs, max_length=256)
|
| 57 |
+
return tokenizer.decode(output[0], skip_special_tokens=True)
|
ffmpeg_test.py
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 2 |
+
|
| 3 |
+
model_id = "Salesforce/codet5p-770m-py"
|
| 4 |
+
|
| 5 |
+
print("Downloading model...")
|
| 6 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 7 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_id)
|
| 8 |
+
print("Download complete.")
|
main.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import uvicorn
|
| 3 |
+
from fastapi import FastAPI, UploadFile, File
|
| 4 |
+
from whisper_model import transcribe_audio
|
| 5 |
+
from codet5_model import generate_code
|
| 6 |
+
from utils import save_audio_file
|
| 7 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 8 |
+
|
| 9 |
+
app = FastAPI()
|
| 10 |
+
|
| 11 |
+
origins = [
|
| 12 |
+
"http://localhost:3000",
|
| 13 |
+
"http://127.0.0.1:3000",
|
| 14 |
+
]
|
| 15 |
+
|
| 16 |
+
app.add_middleware(
|
| 17 |
+
CORSMiddleware,
|
| 18 |
+
allow_origins=origins,
|
| 19 |
+
allow_credentials=True,
|
| 20 |
+
allow_methods=["*"],
|
| 21 |
+
allow_headers=["*"],
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
@app.post("/generate-code")
|
| 25 |
+
async def voice_to_code(file: UploadFile = File(...)):
|
| 26 |
+
audio_path = save_audio_file(file)
|
| 27 |
+
instruction = transcribe_audio(audio_path)
|
| 28 |
+
code_output = generate_code(instruction)
|
| 29 |
+
return {
|
| 30 |
+
"instruction": instruction,
|
| 31 |
+
"code": code_output
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
@app.get("/")
|
| 35 |
+
def home():
|
| 36 |
+
return {"message": "Hello, World!"}
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
if __name__ == "__main__":
|
| 40 |
+
port = int(os.environ.get("PORT", 8000)) # use PORT from env if available
|
| 41 |
+
uvicorn.run("main:app", host="0.0.0.0", port=port, reload=True)
|
requirements.txt
ADDED
|
Binary file (1.39 kB). View file
|
|
|
utils.py
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from fastapi import UploadFile
|
| 3 |
+
|
| 4 |
+
AUDIO_DIR = "audio_files"
|
| 5 |
+
|
| 6 |
+
os.makedirs(AUDIO_DIR, exist_ok=True)
|
| 7 |
+
|
| 8 |
+
def save_audio_file(file: UploadFile) -> str:
|
| 9 |
+
file_path = os.path.join(AUDIO_DIR, file.filename)
|
| 10 |
+
with open(file_path, "wb") as buffer:
|
| 11 |
+
buffer.write(file.file.read())
|
| 12 |
+
return file_path
|
whisper_model.py
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
+
# Add FFmpeg directory to PATH
|
| 4 |
+
os.environ["PATH"] += os.pathsep + r"C:\ffmpeg\bin"
|
| 5 |
+
|
| 6 |
+
import whisper
|
| 7 |
+
|
| 8 |
+
model = whisper.load_model("small")
|
| 9 |
+
|
| 10 |
+
def transcribe_audio(file_path: str) -> str:
|
| 11 |
+
result = model.transcribe(file_path)
|
| 12 |
+
return result["text"]
|