Spaces:
Sleeping
Sleeping
Abhinav Deshpande commited on
Gradio + fastapi
Browse files- app.py +14 -9
- requirements.txt +2 -2
app.py
CHANGED
|
@@ -1,21 +1,20 @@
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
from pydantic import BaseModel
|
| 3 |
import google.generativeai as genai
|
|
|
|
| 4 |
|
| 5 |
-
# Configure the
|
| 6 |
-
genai.configure(api_key="
|
| 7 |
|
| 8 |
-
#
|
| 9 |
app = FastAPI()
|
| 10 |
|
| 11 |
-
#
|
| 12 |
class OCRText(BaseModel):
|
| 13 |
ocr_text: str
|
| 14 |
|
| 15 |
-
# Define the /extract endpoint
|
| 16 |
@app.post("/extract")
|
| 17 |
async def extract_data(request: OCRText):
|
| 18 |
-
# Prepare the prompt for Google Gemini (extracted from OCR text)
|
| 19 |
prompt = f"""
|
| 20 |
Identify and extract manufacturing, expiration dates, and MRP from the following text.
|
| 21 |
The dates may be written in dd/mm/yyyy format or as <Month_name> <Year> or <day> <Month_Name> <Year>.
|
|
@@ -25,9 +24,15 @@ async def extract_data(request: OCRText):
|
|
| 25 |
Text: {request.ocr_text}. Reply with the extracted information only.
|
| 26 |
"""
|
| 27 |
|
| 28 |
-
# Call the Google Gemini model to generate the response based on the OCR input
|
| 29 |
model = genai.GenerativeModel("gemini-1.5-flash")
|
| 30 |
response = model.generate_content(prompt)
|
| 31 |
-
|
| 32 |
-
# Process the response and return it
|
| 33 |
return {"message": response.text}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
from pydantic import BaseModel
|
| 3 |
import google.generativeai as genai
|
| 4 |
+
import gradio as gr
|
| 5 |
|
| 6 |
+
# Configure the Google Gemini API key
|
| 7 |
+
genai.configure(api_key="YOUR_API_KEY")
|
| 8 |
|
| 9 |
+
# FastAPI app
|
| 10 |
app = FastAPI()
|
| 11 |
|
| 12 |
+
# Pydantic model for OCR text input
|
| 13 |
class OCRText(BaseModel):
|
| 14 |
ocr_text: str
|
| 15 |
|
|
|
|
| 16 |
@app.post("/extract")
|
| 17 |
async def extract_data(request: OCRText):
|
|
|
|
| 18 |
prompt = f"""
|
| 19 |
Identify and extract manufacturing, expiration dates, and MRP from the following text.
|
| 20 |
The dates may be written in dd/mm/yyyy format or as <Month_name> <Year> or <day> <Month_Name> <Year>.
|
|
|
|
| 24 |
Text: {request.ocr_text}. Reply with the extracted information only.
|
| 25 |
"""
|
| 26 |
|
|
|
|
| 27 |
model = genai.GenerativeModel("gemini-1.5-flash")
|
| 28 |
response = model.generate_content(prompt)
|
|
|
|
|
|
|
| 29 |
return {"message": response.text}
|
| 30 |
+
|
| 31 |
+
# Gradio interface to call FastAPI logic
|
| 32 |
+
def gradio_interface(ocr_text: str):
|
| 33 |
+
# FastAPI will handle this endpoint
|
| 34 |
+
response = extract_data(OCRText(ocr_text=ocr_text))
|
| 35 |
+
return response['message']
|
| 36 |
+
|
| 37 |
+
iface = gr.Interface(fn=gradio_interface, inputs="text", outputs="json")
|
| 38 |
+
iface.launch()
|
requirements.txt
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
fastapi
|
| 2 |
-
uvicorn[gunicorn]
|
| 3 |
google-generativeai
|
| 4 |
gradio
|
| 5 |
-
|
|
|
|
|
|
| 1 |
fastapi
|
|
|
|
| 2 |
google-generativeai
|
| 3 |
gradio
|
| 4 |
+
uvicorn
|
| 5 |
+
pydantic
|