Ali Abdullah
commited on
Update main.py
Browse files
main.py
CHANGED
|
@@ -15,13 +15,12 @@ from docx import Document
|
|
| 15 |
import pandas as pd
|
| 16 |
import PyPDF2
|
| 17 |
|
| 18 |
-
# Load environment variables
|
| 19 |
load_dotenv()
|
| 20 |
|
| 21 |
-
#
|
| 22 |
pytesseract.pytesseract.tesseract_cmd = os.getenv("TESSERACT_CMD", "/usr/bin/tesseract")
|
| 23 |
-
|
| 24 |
-
os.environ["PATH"] += os.pathsep + ffmpeg_path
|
| 25 |
|
| 26 |
app = FastAPI()
|
| 27 |
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
|
@@ -31,8 +30,6 @@ os.makedirs(UPLOAD_DIR, exist_ok=True)
|
|
| 31 |
|
| 32 |
MAX_FILE_SIZE_MB = 10
|
| 33 |
|
| 34 |
-
|
| 35 |
-
# ---------- File Parsing ----------
|
| 36 |
def extract_text_from_file(file_path):
|
| 37 |
ext = os.path.splitext(file_path)[-1].lower()
|
| 38 |
if ext == ".txt":
|
|
@@ -40,7 +37,7 @@ def extract_text_from_file(file_path):
|
|
| 40 |
return f.read()
|
| 41 |
elif ext == ".docx":
|
| 42 |
doc = Document(file_path)
|
| 43 |
-
return "\n".join([
|
| 44 |
elif ext == ".csv":
|
| 45 |
df = pd.read_csv(file_path)
|
| 46 |
return df.to_string(index=False)
|
|
@@ -48,37 +45,30 @@ def extract_text_from_file(file_path):
|
|
| 48 |
with open(file_path, "rb") as f:
|
| 49 |
reader = PyPDF2.PdfReader(f)
|
| 50 |
return "\n".join([page.extract_text() for page in reader.pages if page.extract_text()])
|
| 51 |
-
|
| 52 |
-
return "❌ Unsupported file type."
|
| 53 |
-
|
| 54 |
|
| 55 |
-
# ---------- File Chat Endpoint ----------
|
| 56 |
@app.post("/chat-with-file")
|
| 57 |
async def chat_with_file(file: UploadFile = File(...), question: str = Form(...)):
|
| 58 |
try:
|
| 59 |
contents = await file.read()
|
| 60 |
if len(contents) > MAX_FILE_SIZE_MB * 1024 * 1024:
|
| 61 |
return JSONResponse(status_code=400, content={"error": "❌ File too large. Max 10MB."})
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
with open(file_path, "wb") as f:
|
| 65 |
f.write(contents)
|
| 66 |
-
|
| 67 |
-
file_content = extract_text_from_file(file_path)
|
| 68 |
|
| 69 |
response = client.chat.completions.create(
|
| 70 |
model="llama3-8b-8192",
|
| 71 |
messages=[
|
| 72 |
-
{"role": "system", "content": "
|
| 73 |
-
{"role": "user", "content": f"{
|
| 74 |
]
|
| 75 |
)
|
| 76 |
return {"answer": response.choices[0].message.content}
|
| 77 |
except Exception as e:
|
| 78 |
return JSONResponse(status_code=500, content={"error": str(e)})
|
| 79 |
|
| 80 |
-
|
| 81 |
-
# ---------- URL Chat Endpoint ----------
|
| 82 |
class URLQuery(BaseModel):
|
| 83 |
url: str
|
| 84 |
question: str
|
|
@@ -87,49 +77,41 @@ class URLQuery(BaseModel):
|
|
| 87 |
async def chat_with_url(data: URLQuery):
|
| 88 |
try:
|
| 89 |
loader = WebBaseLoader(data.url, header_template={"User-Agent": "Mozilla/5.0"})
|
| 90 |
-
|
| 91 |
-
|
| 92 |
|
| 93 |
response = client.chat.completions.create(
|
| 94 |
model="llama3-8b-8192",
|
| 95 |
messages=[
|
| 96 |
-
{"role": "system", "content": "
|
| 97 |
-
{"role": "user", "content": f"
|
| 98 |
]
|
| 99 |
)
|
| 100 |
return {"answer": response.choices[0].message.content}
|
| 101 |
except Exception as e:
|
| 102 |
return JSONResponse(status_code=500, content={"error": str(e)})
|
| 103 |
|
| 104 |
-
|
| 105 |
-
# ---------- Image OCR Endpoint ----------
|
| 106 |
@app.post("/extract-text-from-image")
|
| 107 |
async def extract_text_from_image(file: UploadFile = File(...)):
|
| 108 |
try:
|
| 109 |
contents = await file.read()
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
image = image.resize((image.width * 2, image.height * 2)) # Upscale for better OCR
|
| 114 |
-
|
| 115 |
-
text = pytesseract.image_to_string(image, lang='eng')
|
| 116 |
return {"answer": text.strip() or "⚠️ No text extracted."}
|
| 117 |
except Exception as e:
|
| 118 |
return JSONResponse(status_code=500, content={"error": str(e)})
|
| 119 |
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
# ---------- Audio Transcription Endpoint ----------
|
| 123 |
@app.post("/transcribe-audio")
|
| 124 |
async def transcribe_audio(file: UploadFile = File(...)):
|
| 125 |
try:
|
| 126 |
contents = await file.read()
|
| 127 |
-
|
| 128 |
-
with open(
|
| 129 |
f.write(contents)
|
| 130 |
|
| 131 |
model = whisper.load_model("base")
|
| 132 |
-
result = model.transcribe(
|
| 133 |
-
return {"answer": result
|
| 134 |
except Exception as e:
|
| 135 |
return JSONResponse(status_code=500, content={"error": str(e)})
|
|
|
|
| 15 |
import pandas as pd
|
| 16 |
import PyPDF2
|
| 17 |
|
| 18 |
+
# Load environment variables
|
| 19 |
load_dotenv()
|
| 20 |
|
| 21 |
+
# Set paths for OCR & audio
|
| 22 |
pytesseract.pytesseract.tesseract_cmd = os.getenv("TESSERACT_CMD", "/usr/bin/tesseract")
|
| 23 |
+
os.environ["PATH"] += os.pathsep + os.getenv("FFMPEG_PATH", "/usr/bin")
|
|
|
|
| 24 |
|
| 25 |
app = FastAPI()
|
| 26 |
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
|
|
|
| 30 |
|
| 31 |
MAX_FILE_SIZE_MB = 10
|
| 32 |
|
|
|
|
|
|
|
| 33 |
def extract_text_from_file(file_path):
|
| 34 |
ext = os.path.splitext(file_path)[-1].lower()
|
| 35 |
if ext == ".txt":
|
|
|
|
| 37 |
return f.read()
|
| 38 |
elif ext == ".docx":
|
| 39 |
doc = Document(file_path)
|
| 40 |
+
return "\n".join([p.text for p in doc.paragraphs])
|
| 41 |
elif ext == ".csv":
|
| 42 |
df = pd.read_csv(file_path)
|
| 43 |
return df.to_string(index=False)
|
|
|
|
| 45 |
with open(file_path, "rb") as f:
|
| 46 |
reader = PyPDF2.PdfReader(f)
|
| 47 |
return "\n".join([page.extract_text() for page in reader.pages if page.extract_text()])
|
| 48 |
+
return "❌ Unsupported file type."
|
|
|
|
|
|
|
| 49 |
|
|
|
|
| 50 |
@app.post("/chat-with-file")
|
| 51 |
async def chat_with_file(file: UploadFile = File(...), question: str = Form(...)):
|
| 52 |
try:
|
| 53 |
contents = await file.read()
|
| 54 |
if len(contents) > MAX_FILE_SIZE_MB * 1024 * 1024:
|
| 55 |
return JSONResponse(status_code=400, content={"error": "❌ File too large. Max 10MB."})
|
| 56 |
+
path = os.path.join(UPLOAD_DIR, file.filename)
|
| 57 |
+
with open(path, "wb") as f:
|
|
|
|
| 58 |
f.write(contents)
|
| 59 |
+
text = extract_text_from_file(path)
|
|
|
|
| 60 |
|
| 61 |
response = client.chat.completions.create(
|
| 62 |
model="llama3-8b-8192",
|
| 63 |
messages=[
|
| 64 |
+
{"role": "system", "content": "You are a helpful assistant. Answer using the uploaded file."},
|
| 65 |
+
{"role": "user", "content": f"{text}\n\nQuestion: {question}"}
|
| 66 |
]
|
| 67 |
)
|
| 68 |
return {"answer": response.choices[0].message.content}
|
| 69 |
except Exception as e:
|
| 70 |
return JSONResponse(status_code=500, content={"error": str(e)})
|
| 71 |
|
|
|
|
|
|
|
| 72 |
class URLQuery(BaseModel):
|
| 73 |
url: str
|
| 74 |
question: str
|
|
|
|
| 77 |
async def chat_with_url(data: URLQuery):
|
| 78 |
try:
|
| 79 |
loader = WebBaseLoader(data.url, header_template={"User-Agent": "Mozilla/5.0"})
|
| 80 |
+
docs = loader.load()
|
| 81 |
+
content = "\n".join([doc.page_content for doc in docs])
|
| 82 |
|
| 83 |
response = client.chat.completions.create(
|
| 84 |
model="llama3-8b-8192",
|
| 85 |
messages=[
|
| 86 |
+
{"role": "system", "content": "You are a helpful assistant. Answer using the webpage content."},
|
| 87 |
+
{"role": "user", "content": f"Web Content:\n{content}\n\nQuestion: {data.question}"}
|
| 88 |
]
|
| 89 |
)
|
| 90 |
return {"answer": response.choices[0].message.content}
|
| 91 |
except Exception as e:
|
| 92 |
return JSONResponse(status_code=500, content={"error": str(e)})
|
| 93 |
|
|
|
|
|
|
|
| 94 |
@app.post("/extract-text-from-image")
|
| 95 |
async def extract_text_from_image(file: UploadFile = File(...)):
|
| 96 |
try:
|
| 97 |
contents = await file.read()
|
| 98 |
+
image = Image.open(io.BytesIO(contents)).convert("L")
|
| 99 |
+
image = image.resize((image.width * 2, image.height * 2))
|
| 100 |
+
text = pytesseract.image_to_string(image, lang="eng")
|
|
|
|
|
|
|
|
|
|
| 101 |
return {"answer": text.strip() or "⚠️ No text extracted."}
|
| 102 |
except Exception as e:
|
| 103 |
return JSONResponse(status_code=500, content={"error": str(e)})
|
| 104 |
|
|
|
|
|
|
|
|
|
|
| 105 |
@app.post("/transcribe-audio")
|
| 106 |
async def transcribe_audio(file: UploadFile = File(...)):
|
| 107 |
try:
|
| 108 |
contents = await file.read()
|
| 109 |
+
path = os.path.join(UPLOAD_DIR, file.filename)
|
| 110 |
+
with open(path, "wb") as f:
|
| 111 |
f.write(contents)
|
| 112 |
|
| 113 |
model = whisper.load_model("base")
|
| 114 |
+
result = model.transcribe(path)
|
| 115 |
+
return {"answer": result.get("text", "⚠️ No transcript returned.")}
|
| 116 |
except Exception as e:
|
| 117 |
return JSONResponse(status_code=500, content={"error": str(e)})
|