Update app.py
Browse files
app.py
CHANGED
@@ -1,63 +1,104 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
):
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
)
|
58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
)
|
60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
-
|
63 |
-
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import LLaMAForConditionalGeneration, LLaMATokenizer
|
3 |
+
import torch
|
4 |
+
import pandas as pd
|
5 |
+
from PyPDF2 import PdfFileReader
|
6 |
+
from googleapiclient.discovery import build
|
7 |
+
from google_auth_oauthlib.flow import InstalledAppFlow
|
8 |
+
from google.auth.transport.requests import Request
|
9 |
+
import pickle
|
10 |
+
import pydub
|
11 |
+
|
12 |
+
# Set up LLaMA model and tokenizer
|
13 |
+
model = LLaMAForConditionalGeneration.from_pretrained("facebook/llama-3.1-base")
|
14 |
+
tokenizer = LLaMATokenizer.from_pretrained("facebook/llama-3.1-base")
|
15 |
+
|
16 |
+
# Set up Google API credentials
|
17 |
+
SCOPES = ['https://www.googleapis.com/auth/drive']
|
18 |
+
creds = None
|
19 |
+
if creds is None or not creds.valid:
|
20 |
+
if creds and creds.expired and creds.refresh_token:
|
21 |
+
creds.refresh(Request())
|
22 |
+
else:
|
23 |
+
flow = InstalledAppFlow.from_client_secrets_file(
|
24 |
+
'credentials.json', SCOPES)
|
25 |
+
creds = flow.run_local_server(port=0)
|
26 |
+
drive_service = build('drive', 'v3', credentials=creds)
|
27 |
+
|
28 |
+
# Define function to process uploaded files
|
29 |
+
def process_file(file):
|
30 |
+
if file.name.endswith('.pdf'):
|
31 |
+
pdf_file = PdfFileReader(file)
|
32 |
+
text = ''
|
33 |
+
for page in range(pdf_file.numPages):
|
34 |
+
text += pdf_file.getPage(page).extractText()
|
35 |
+
return text
|
36 |
+
elif file.name.endswith('.csv') or file.name.endswith('.xlsx'):
|
37 |
+
if file.name.endswith('.csv'):
|
38 |
+
df = pd.read_csv(file)
|
39 |
+
else:
|
40 |
+
df = pd.read_excel(file)
|
41 |
+
return str(df)
|
42 |
+
elif file.name.endswith('.docx'):
|
43 |
+
# You need to implement a function to extract text from Word documents
|
44 |
+
# For simplicity, this example just returns an error message
|
45 |
+
return "Error: Word document support not implemented"
|
46 |
+
elif file.name.endswith('.gsheet'):
|
47 |
+
spreadsheet_id = file.name.split('/')[-1]
|
48 |
+
range_name = 'Sheet1!A1:Z1000' # You can change this range as needed
|
49 |
+
service = build('sheets', 'v4', credentials=creds)
|
50 |
+
sheet = service.spreadsheets()
|
51 |
+
result = sheet.values().get(spreadsheetId=spreadsheet_id,
|
52 |
+
range=range_name).execute()
|
53 |
+
values = result.get('values', [])
|
54 |
+
return str(values)
|
55 |
+
elif file.name.endswith('.gdoc'):
|
56 |
+
document_id = file.name.split('/')[-1]
|
57 |
+
service = build('docs', 'v1', credentials=creds)
|
58 |
+
doc = service.documents().get(documentId=document_id).execute()
|
59 |
+
text = ''
|
60 |
+
for element in doc.get('body').get('content'):
|
61 |
+
if 'paragraph' in element:
|
62 |
+
text += element.get('paragraph').get('elements')[0].get('textRun').get('content')
|
63 |
+
return text
|
64 |
+
elif file.name.endswith('.mp3'):
|
65 |
+
audio = pydub.AudioSegment.from_mp3(file)
|
66 |
+
text = ''
|
67 |
+
# You need to implement a function to transcribe audio
|
68 |
+
# For simplicity, this example just returns an error message
|
69 |
+
return "Error: Audio transcription support not implemented"
|
70 |
+
else:
|
71 |
+
return "Error: File type not supported"
|
72 |
+
|
73 |
+
# Define function to answer questions about the uploaded content
|
74 |
+
def answer_question(content, question):
|
75 |
+
inputs = tokenizer.encode(question, return_tensors="pt")
|
76 |
+
outputs = model.generate(inputs, max_length=100)
|
77 |
+
answer = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
78 |
+
return answer
|
79 |
+
|
80 |
+
# Create Gradio interface
|
81 |
+
demo = gr.Interface(
|
82 |
+
fn=answer_question,
|
83 |
+
inputs=["file", "text"],
|
84 |
+
outputs="text",
|
85 |
+
title="LLaMA Chatbot",
|
86 |
+
description="Upload a file or paste some text, and ask a question about the content.",
|
87 |
)
|
88 |
|
89 |
+
# Define function to update the Gradio interface with the uploaded file's content
|
90 |
+
def update_interface(file):
|
91 |
+
content = process_file(file)
|
92 |
+
demo.update(inputs=[content])
|
93 |
+
|
94 |
+
# Create Gradio interface with file upload
|
95 |
+
demo_with_upload = gr.Interface(
|
96 |
+
fn=update_interface,
|
97 |
+
inputs=["file"],
|
98 |
+
outputs=None,
|
99 |
+
title="LLaMA Chatbot",
|
100 |
+
description="Upload a file to analyze its content.",
|
101 |
+
)
|
102 |
|
103 |
+
# Launch Gradio interface
|
104 |
+
demo_with_upload.launch()
|