1
+ from fastapi import FastAPI , UploadFile , File
2
+ from fastapi .responses import JSONResponse
3
+ import uvicorn
4
+ import os
5
+ from groq import Groq
6
+ import PyPDF2
7
+ from pptx import Presentation
8
+ import io
9
+ from PIL import Image
10
+ import tempfile
11
+ import base64
12
+ from mangum import Mangum
13
+
14
+ app = FastAPI ()
15
+ handler = Mangum (app )
16
+ # Initialize Groq client
17
+ client = Groq (
18
+ api_key = "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" ,
19
+ )
20
+
21
+ @app .get ("/" )
22
+ async def root ():
23
+ return {"message" : "Welcome" }
24
+
25
+ @app .get ("/greeting" )
26
+ async def greeting ():
27
+ return {"message" : "Hello" , "details" : "You are using LAamAScholar Bot" }
28
+
29
+ @app .post ("/summarize" )
30
+ async def summarize_file (file : UploadFile = File (...)):
31
+ content = await file .read ()
32
+
33
+ if file .filename .endswith ('.pdf' ):
34
+ text = extract_text_from_pdf (content )
35
+ elif file .filename .endswith ('.pptx' ):
36
+ text = extract_text_from_pptx (content )
37
+ else :
38
+ return JSONResponse (status_code = 400 , content = {"error" : "Unsupported file format. Please upload a PDF or PPTX file." })
39
+
40
+ summary = summarize_text (text )
41
+ return {"summary" : summary }
42
+
43
+ @app .post ("/generate_questions" )
44
+ async def generate_questions (file : UploadFile = File (...)):
45
+ content = await file .read ()
46
+
47
+ if file .filename .endswith ('.pdf' ):
48
+ text = extract_text_from_pdf (content )
49
+ elif file .filename .endswith ('.pptx' ):
50
+ text = extract_text_from_pptx (content )
51
+ else :
52
+ return JSONResponse (status_code = 400 , content = {"error" : "Unsupported file format. Please upload a PDF or PPTX file." })
53
+
54
+ questions = generate_important_questions (text )
55
+ return {"questions" : questions }
56
+
57
+ @app .post ("/voice-notes" )
58
+ async def transcribe_voice_notes (file : UploadFile = File (...)):
59
+ if not file .filename .endswith ('.mp3' ):
60
+ return JSONResponse (status_code = 400 , content = {"error" : "Unsupported file format. Please upload an MP3 file." })
61
+
62
+ # Create a temporary file to store the uploaded content
63
+ with tempfile .NamedTemporaryFile (delete = False , suffix = ".mp3" ) as temp_file :
64
+ content = await file .read ()
65
+ temp_file .write (content )
66
+ temp_file_path = temp_file .name
67
+
68
+ try :
69
+ with open (temp_file_path , "rb" ) as audio_file :
70
+ transcription = client .audio .transcriptions .create (
71
+ file = (file .filename , audio_file ),
72
+ model = "whisper-large-v3" ,
73
+ response_format = "verbose_json" ,
74
+ )
75
+
76
+ return {"transcription" : transcription .text }
77
+ finally :
78
+ # Clean up the temporary file
79
+ os .unlink (temp_file_path )
80
+ def encode_image (image_file ):
81
+ return base64 .b64encode (image_file .read ()).decode ('utf-8' )
82
+
83
+ @app .post ("/ocr/" )
84
+ async def perform_ocr (file : UploadFile = File (...)):
85
+ # Read and encode the image
86
+ contents = await file .read ()
87
+ base64_image = encode_image (io .BytesIO (contents ))
88
+
89
+ # Prepare the message for Groq
90
+ messages = [
91
+ {
92
+ "role" : "user" ,
93
+ "content" : [
94
+ {
95
+ "type" : "image_url" ,
96
+ "image_url" : {
97
+ "url" : f"data:image/jpeg;base64,{ base64_image } "
98
+ }
99
+ },
100
+ {
101
+ "type" : "text" ,
102
+ "text" : "Explain what is written in the image from an educational Point of View."
103
+ }
104
+ ]
105
+ }
106
+ ]
107
+
108
+ # Make the API call to Groq
109
+ completion = client .chat .completions .create (
110
+ model = "llama-3.2-11b-vision-preview" ,
111
+ messages = messages ,
112
+ temperature = 0 , # Set to 0 for more deterministic results
113
+ max_tokens = 1024 ,
114
+ top_p = 1 ,
115
+ stream = False ,
116
+ stop = None ,
117
+ )
118
+
119
+ # Extract the OCR result from the response
120
+ ocr_result = completion .choices [0 ].message .content
121
+
122
+ return {"ocr_result" : ocr_result }
123
+
124
+ def extract_text_from_pdf (content ):
125
+ pdf_reader = PyPDF2 .PdfReader (io .BytesIO (content ))
126
+ text = ""
127
+ for page in pdf_reader .pages :
128
+ text += page .extract_text ()
129
+ return text
130
+
131
+ def extract_text_from_pptx (content ):
132
+ prs = Presentation (io .BytesIO (content ))
133
+ text = ""
134
+ for slide in prs .slides :
135
+ for shape in slide .shapes :
136
+ if hasattr (shape , 'text' ):
137
+ text += shape .text + "\n "
138
+ return text
139
+
140
+ def summarize_text (text ):
141
+ prompt = f"Summarize the following text:\n \n { text [:4000 ]} ..." # Truncate to 4000 characters to fit within token limit
142
+
143
+ chat_completion = client .chat .completions .create (
144
+ messages = [
145
+ {
146
+ "role" : "user" ,
147
+ "content" : prompt ,
148
+ }
149
+ ],
150
+ model = "llama3-8b-8192" ,
151
+ )
152
+
153
+ return chat_completion .choices [0 ].message .content
154
+ def generate_important_questions (text ):
155
+ prompt = f"Generate 5 important questions based on the following text:\n \n { text [:4000 ]} ..." # Truncate to 4000 characters to fit within token limit
156
+
157
+ chat_completion = client .chat .completions .create (
158
+ messages = [
159
+ {
160
+ "role" : "user" ,
161
+ "content" : prompt ,
162
+ }
163
+ ],
164
+ model = "llama3-8b-8192" ,
165
+ )
166
+
167
+ return chat_completion .choices [0 ].message .content
168
+
169
+ if __name__ == "__main__" :
170
+ uvicorn .run (app , host = "0.0.0.0" , port = 8000 )
0 commit comments