aakash0563 commited on
Commit
8b4a847
1 Parent(s): a5372c2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -74
app.py CHANGED
@@ -1,97 +1,97 @@
1
- # import google.generativeai as genai
2
- # from PIL import Image
3
- # import gradio as gr
4
- # import numpy as np
5
- # import os
6
 
7
- # GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
8
 
9
- # # Now you can use hugging_face_api_key in your code
10
 
11
- # genai.configure(api_key=GOOGLE_API_KEY)
12
 
13
- # model = genai.GenerativeModel('gemini-pro-vision')
14
- # def process_image_and_text(image, text):
15
- # # Assuming image is the input from Gradio
16
- # if text:
17
- # image_array = np.asarray(image.data) # Convert memoryview to NumPy array
18
- # image = Image.fromarray(image_array.astype('uint8'), 'RGB') # Now you can use astype
19
- # response = model.generate_content([text, image])
20
- # return response.text
21
- # else:
22
- # image_array = np.asarray(image.data) # Convert memoryview to NumPy array
23
- # image = Image.fromarray(image_array.astype('uint8'), 'RGB') # Now you can use astype
24
- # response = model.generate_content(["Tell me about this image in bulletin format", image])
25
- # return response.text
26
 
27
 
28
- # iface = gr.Interface(
29
- # process_image_and_text,
30
- # inputs=["image", "textbox"], # Specify image and text inputs
31
- # outputs="textbox", # Specify text output
32
- # title="Image and Text Processor", # Set the app title
33
- # )
34
 
35
- # iface.launch(debug=True, share=True) # Launch the Gradio app
36
 
37
 
38
 
39
- import google.generativeai as genai
40
- import os
41
- import os
42
- from pdf2image import convert_from_path
43
- from PIL import Image
44
- import pdf2image
45
- import numpy as np
46
- GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
47
 
48
- # Now you can use hugging_face_api_key in your code
49
 
50
- genai.configure(api_key=GOOGLE_API_KEY)
51
 
52
 
53
- import gradio as gr
54
- # print(llm.predict("Who is the PM of India?"))
55
- model = genai.GenerativeModel('gemini-pro-vision')
56
- def process_image_and_text(images):
57
- response = {}
58
- for i,image in enumerate(images):
59
- # # Assuming image is the input from Gradio
60
- # image_array = np.asarray(image.data) # Convert memoryview to NumPy array
61
- # image = Image.fromarray(image_array.astype('uint8'), 'RGB') # Now you can use astype
62
- response = model.generate_content(["You are act as a tutor Solve all the question in the image in step by step: ", image])
63
- response[i] = response.text
64
- return response
65
 
66
- def input_pdf_setup(uploaded_pdf):
67
- # Convert PDF pages to images
68
- images = convert_from_path(uploaded_pdf, dpi=200)
69
- return images
70
 
71
 
72
 
73
- def extract_answer(uploaded_pdf):
74
- """Retrieves answers from processed images and presents them clearly."""
75
- images = input_pdf_setup(uploaded_pdf)
76
- responses = process_image_and_text(images=images)
77
 
78
- # Present results in a user-friendly format
79
- answers = []
80
- for i, response in enumerate(responses.values()):
81
- answers.append(f"Answer for question {i+1}:\n {response}")
82
 
83
- return "\n".join(answers)
84
 
85
- # Create Gradio interface
86
- iface = gr.Interface(
87
- fn=extract_answer,
88
- inputs="file",
89
- outputs="text",
90
- title="Question-Answering with Gemstone.ai",
91
- description="Upload a PDF containing questions, and get step-by-step answers!",
92
- allow_flagging=True,
93
- )
94
 
95
 
96
- # Launch the Gradio application
97
- iface.launch(share=True, debug=True)
 
1
+ import google.generativeai as genai
2
+ from PIL import Image
3
+ import gradio as gr
4
+ import numpy as np
5
+ import os
6
 
7
+ GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
8
 
9
+ # Now you can use hugging_face_api_key in your code
10
 
11
+ genai.configure(api_key=GOOGLE_API_KEY)
12
 
13
+ model = genai.GenerativeModel('gemini-pro-vision')
14
+ def process_image_and_text(image, text):
15
+ # Assuming image is the input from Gradio
16
+ if text:
17
+ image_array = np.asarray(image.data) # Convert memoryview to NumPy array
18
+ image = Image.fromarray(image_array.astype('uint8'), 'RGB') # Now you can use astype
19
+ response = model.generate_content([text, image])
20
+ return response.text
21
+ else:
22
+ image_array = np.asarray(image.data) # Convert memoryview to NumPy array
23
+ image = Image.fromarray(image_array.astype('uint8'), 'RGB') # Now you can use astype
24
+ response = model.generate_content(["Tell me about this image in bulletin format", image])
25
+ return response.text
26
 
27
 
28
+ iface = gr.Interface(
29
+ process_image_and_text,
30
+ inputs=["image", "textbox"], # Specify image and text inputs
31
+ outputs="textbox", # Specify text output
32
+ title="Image and Text Processor", # Set the app title
33
+ )
34
 
35
+ iface.launch(debug=True, share=True) # Launch the Gradio app
36
 
37
 
38
 
39
+ # import google.generativeai as genai
40
+ # import os
41
+ # import os
42
+ # from pdf2image import convert_from_path
43
+ # from PIL import Image
44
+ # import pdf2image
45
+ # import numpy as np
46
+ # GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
47
 
48
+ # # Now you can use hugging_face_api_key in your code
49
 
50
+ # genai.configure(api_key=GOOGLE_API_KEY)
51
 
52
 
53
+ # import gradio as gr
54
+ # # print(llm.predict("Who is the PM of India?"))
55
+ # model = genai.GenerativeModel('gemini-pro-vision')
56
+ # def process_image_and_text(images):
57
+ # response = {}
58
+ # for i,image in enumerate(images):
59
+ # # # Assuming image is the input from Gradio
60
+ # # image_array = np.asarray(image.data) # Convert memoryview to NumPy array
61
+ # # image = Image.fromarray(image_array.astype('uint8'), 'RGB') # Now you can use astype
62
+ # response = model.generate_content(["You are act as a tutor Solve all the question in the image in step by step: ", image])
63
+ # response[i] = response.text
64
+ # return response
65
 
66
+ # def input_pdf_setup(uploaded_pdf):
67
+ # # Convert PDF pages to images
68
+ # images = convert_from_path(uploaded_pdf, dpi=200)
69
+ # return images
70
 
71
 
72
 
73
+ # def extract_answer(uploaded_pdf):
74
+ # """Retrieves answers from processed images and presents them clearly."""
75
+ # images = input_pdf_setup(uploaded_pdf)
76
+ # responses = process_image_and_text(images=images)
77
 
78
+ # # Present results in a user-friendly format
79
+ # answers = []
80
+ # for i, response in enumerate(responses.values()):
81
+ # answers.append(f"Answer for question {i+1}:\n {response}")
82
 
83
+ # return "\n".join(answers)
84
 
85
+ # # Create Gradio interface
86
+ # iface = gr.Interface(
87
+ # fn=extract_answer,
88
+ # inputs="file",
89
+ # outputs="text",
90
+ # title="Question-Answering with Gemstone.ai",
91
+ # description="Upload a PDF containing questions, and get step-by-step answers!",
92
+ # allow_flagging=True,
93
+ # )
94
 
95
 
96
+ # # Launch the Gradio application
97
+ # iface.launch(share=True, debug=True)