Abhinav Deshpande commited on
Commit
ba19677
·
unverified ·
1 Parent(s): 3fd920e

Gradio + fastapi

Browse files
Files changed (2) hide show
  1. app.py +14 -9
  2. 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 API key for Google Gemini
6
- genai.configure(api_key="AIzaSyCZKxLPz1lcO5QrATDYbgJmOZHOk4aIKuA")
7
 
8
- # Create a FastAPI instance
9
  app = FastAPI()
10
 
11
- # Define the OCRText model to accept input
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
- pydantic
 
 
1
  fastapi
 
2
  google-generativeai
3
  gradio
4
+ uvicorn
5
+ pydantic