Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
CHANGED
@@ -1,63 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
-
|
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 pandas as pd
|
2 |
+
from PyPDF2 import PdfReader
|
3 |
+
from docx import Document
|
4 |
+
import os
|
5 |
+
from groq import Groq
|
6 |
import gradio as gr
|
7 |
+
|
8 |
+
# Function to read and process different document types
|
9 |
+
def read_document(file):
|
10 |
+
try:
|
11 |
+
file_extension = os.path.splitext(file.name)[-1].lower()
|
12 |
+
print(f"Processing file: {file.name} with extension {file_extension}")
|
13 |
+
|
14 |
+
if file_extension == '.txt':
|
15 |
+
return file.read().decode('utf-8')
|
16 |
+
elif file_extension == '.pdf':
|
17 |
+
reader = PdfReader(file)
|
18 |
+
text = ''
|
19 |
+
for page in reader.pages:
|
20 |
+
text += page.extract_text()
|
21 |
+
return text
|
22 |
+
elif file_extension == '.docx':
|
23 |
+
doc = Document(file)
|
24 |
+
return '\n'.join([paragraph.text for paragraph in doc.paragraphs])
|
25 |
+
elif file_extension in ['.csv', '.xls', '.xlsx']:
|
26 |
+
df = pd.read_excel(file) if file_extension != '.csv' else pd.read_csv(file)
|
27 |
+
return df.to_string(index=False)
|
28 |
+
else:
|
29 |
+
return "Unsupported file format"
|
30 |
+
except Exception as e:
|
31 |
+
print(f"Error processing file: {file.name} - {str(e)}")
|
32 |
+
return f"Error processing file: {file.name} - {str(e)}"
|
33 |
+
|
34 |
+
# Set your API key as an environment variable
|
35 |
+
GROQ_API_KEY = "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" # Replace with your actual API key
|
36 |
+
|
37 |
+
client = Groq()
|
38 |
+
|
39 |
+
# Function to validate and truncate content to prevent API errors
|
40 |
+
def validate_content(text):
|
41 |
+
# Basic validation to remove unwanted characters
|
42 |
+
validated_text = ''.join(e for e in text if e.isalnum() or e.isspace())
|
43 |
+
# Truncate text if it's too long
|
44 |
+
max_length = 8000 # Adjust as needed
|
45 |
+
if len(validated_text) > max_length:
|
46 |
+
validated_text = validated_text[:max_length] + "..."
|
47 |
+
return validated_text
|
48 |
+
|
49 |
+
# Function to get an answer from the Groq API
|
50 |
+
def get_answer(question, model="llama3-8b-8192"):
|
51 |
+
try:
|
52 |
+
chat_completion = client.chat.completions.create(
|
53 |
+
model=model,
|
54 |
+
messages=[{"role": "user", "content": question}],
|
55 |
+
)
|
56 |
+
return chat_completion.choices[0].message.content
|
57 |
+
except Exception as e:
|
58 |
+
print(f"Error in Groq API call: {str(e)}")
|
59 |
+
if hasattr(e, 'response'):
|
60 |
+
print(f"Full response: {e.response.json()}")
|
61 |
+
return f"Error in API call: {str(e)}"
|
62 |
+
|
63 |
+
# Function to interface with the Gradio UI
|
64 |
+
def chatbot_interface(documents, question):
|
65 |
+
text = ''
|
66 |
+
for doc in documents:
|
67 |
+
content = read_document(doc)
|
68 |
+
text += validate_content(content) + "\n\n"
|
69 |
+
|
70 |
+
answer = get_answer(f"{text}\n\nQuestion: {question}")
|
71 |
+
return answer
|
72 |
+
|
73 |
+
# Gradio Interface
|
74 |
+
with gr.Blocks(theme=gr.themes.Default(primary_hue="slate")) as demo:
|
75 |
+
gr.Markdown("# RAG-based Q/A Chatbot with Document Support", elem_id="title")
|
76 |
+
gr.Markdown("Upload documents and ask questions related to them.", elem_id="description")
|
77 |
+
|
78 |
+
with gr.Row():
|
79 |
+
with gr.Column():
|
80 |
+
doc_input = gr.File(file_count="multiple", label="Upload Documents")
|
81 |
+
question_input = gr.Textbox(label="Ask a Question")
|
82 |
+
|
83 |
+
with gr.Column():
|
84 |
+
output = gr.Textbox(label="Answer")
|
85 |
+
|
86 |
+
submit_button = gr.Button("Get Answer")
|
87 |
+
submit_button.click(chatbot_interface, inputs=[doc_input, question_input], outputs=output)
|
88 |
+
|
89 |
+
demo.launch()
|