Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,6 +7,8 @@ import io
|
|
| 7 |
import base64
|
| 8 |
from fpdf import FPDF
|
| 9 |
from textblob import TextBlob
|
|
|
|
|
|
|
| 10 |
|
| 11 |
nltk.download("punkt", quiet=True)
|
| 12 |
|
|
@@ -14,37 +16,32 @@ nltk.download("punkt", quiet=True)
|
|
| 14 |
# Hugging Face Chat Code #
|
| 15 |
###############################################################################
|
| 16 |
"""
|
| 17 |
-
For more information on
|
| 18 |
https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 19 |
"""
|
| 20 |
|
| 21 |
# Initialize your Hugging Face model client
|
| 22 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 23 |
|
| 24 |
-
def respond(
|
| 25 |
-
message,
|
| 26 |
-
history: list[tuple[str, str]],
|
| 27 |
-
system_message,
|
| 28 |
-
max_tokens,
|
| 29 |
-
temperature,
|
| 30 |
-
top_p
|
| 31 |
-
):
|
| 32 |
"""
|
| 33 |
-
Streams the chat response from the Hugging Face model.
|
| 34 |
-
|
|
|
|
| 35 |
"""
|
| 36 |
-
#
|
| 37 |
-
|
|
|
|
| 38 |
|
|
|
|
|
|
|
| 39 |
for val in history:
|
| 40 |
if val[0]:
|
| 41 |
messages.append({"role": "user", "content": val[0]})
|
| 42 |
if val[1]:
|
| 43 |
messages.append({"role": "assistant", "content": val[1]})
|
| 44 |
-
|
| 45 |
messages.append({"role": "user", "content": message})
|
| 46 |
|
| 47 |
-
# Streaming response
|
| 48 |
response = ""
|
| 49 |
for partial in client.chat_completion(
|
| 50 |
messages,
|
|
@@ -58,158 +55,91 @@ def respond(
|
|
| 58 |
yield response
|
| 59 |
|
| 60 |
###############################################################################
|
| 61 |
-
#
|
| 62 |
###############################################################################
|
| 63 |
|
| 64 |
-
def
|
| 65 |
-
"""Splits the text into sentences using nltk."""
|
| 66 |
-
return [s.strip() for s in nltk.sent_tokenize(text) if s.strip()]
|
| 67 |
-
|
| 68 |
-
def generate_comments(sentences):
|
| 69 |
"""
|
| 70 |
-
|
| 71 |
-
|
|
|
|
| 72 |
"""
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
pdf.add_page()
|
| 90 |
-
pdf.set_auto_page_break(auto=True, margin=15)
|
| 91 |
-
pdf.set_font("Arial", size=12)
|
| 92 |
-
|
| 93 |
-
for s, c in zip(sentences, comments):
|
| 94 |
-
pdf.multi_cell(0, 10, f"Sentence: {s}", 0, 1)
|
| 95 |
-
pdf.multi_cell(0, 10, c, 0, 1)
|
| 96 |
-
pdf.ln(5)
|
| 97 |
-
|
| 98 |
-
pdf_buffer = io.BytesIO()
|
| 99 |
-
pdf.output(pdf_buffer, 'F')
|
| 100 |
-
pdf_buffer.seek(0)
|
| 101 |
-
return pdf_buffer
|
| 102 |
|
| 103 |
-
def
|
| 104 |
"""
|
| 105 |
-
|
| 106 |
-
|
| 107 |
"""
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
if output_format == "JSON":
|
| 115 |
-
# Return JSON text, no file
|
| 116 |
-
json_data = convert_to_json(sentences, comments)
|
| 117 |
-
return json_data, None
|
| 118 |
-
else:
|
| 119 |
-
# Return PDF as bytes, no text
|
| 120 |
-
pdf_buffer = convert_to_pdf(sentences, comments)
|
| 121 |
-
# Gradio expects a tuple: (file_name, file_bytes)
|
| 122 |
-
return None, ("output.pdf", pdf_buffer.getvalue())
|
| 123 |
|
| 124 |
###############################################################################
|
| 125 |
-
#
|
| 126 |
###############################################################################
|
| 127 |
|
| 128 |
with gr.Blocks() as demo:
|
| 129 |
-
gr.Markdown("#
|
| 130 |
gr.Markdown(
|
| 131 |
"""
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
|
|
|
| 135 |
"""
|
| 136 |
)
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
placeholder="Type or paste your text here...",
|
| 173 |
-
lines=10,
|
| 174 |
-
)
|
| 175 |
-
format_dropdown = gr.Dropdown(
|
| 176 |
-
choices=["JSON", "PDF"],
|
| 177 |
-
value="JSON",
|
| 178 |
-
label="Choose output format",
|
| 179 |
-
)
|
| 180 |
-
|
| 181 |
-
convert_button = gr.Button("Convert")
|
| 182 |
-
|
| 183 |
-
# Two possible outputs: either JSON text or a PDF file
|
| 184 |
-
output_json = gr.Code(
|
| 185 |
-
label="JSON Output",
|
| 186 |
-
language="json",
|
| 187 |
-
visible=True,
|
| 188 |
-
)
|
| 189 |
-
output_file = gr.File(label="PDF Download")
|
| 190 |
-
|
| 191 |
-
def run_conversion(text, fmt):
|
| 192 |
-
"""
|
| 193 |
-
Helper function to connect with Gradio.
|
| 194 |
-
Returns either a JSON string or a PDF file handle.
|
| 195 |
-
"""
|
| 196 |
-
json_str, pdf_file = process_text(text, fmt)
|
| 197 |
-
# If we got an error or JSON
|
| 198 |
-
if isinstance(json_str, str) and json_str.startswith("Error:"):
|
| 199 |
-
return json_str, None
|
| 200 |
-
if fmt == "JSON":
|
| 201 |
-
# Show JSON in the code area, no file
|
| 202 |
-
return json_str, None
|
| 203 |
-
else:
|
| 204 |
-
# Return no text, but a file
|
| 205 |
-
return None, pdf_file
|
| 206 |
-
|
| 207 |
-
convert_button.click(
|
| 208 |
-
fn=run_conversion,
|
| 209 |
-
inputs=[input_text, format_dropdown],
|
| 210 |
-
outputs=[output_json, output_file],
|
| 211 |
-
)
|
| 212 |
-
|
| 213 |
-
# Launch the Gradio app
|
| 214 |
if __name__ == "__main__":
|
| 215 |
demo.launch()
|
|
|
|
| 7 |
import base64
|
| 8 |
from fpdf import FPDF
|
| 9 |
from textblob import TextBlob
|
| 10 |
+
import PyPDF2
|
| 11 |
+
import tempfile
|
| 12 |
|
| 13 |
nltk.download("punkt", quiet=True)
|
| 14 |
|
|
|
|
| 16 |
# Hugging Face Chat Code #
|
| 17 |
###############################################################################
|
| 18 |
"""
|
| 19 |
+
For more information on Hugging Face Inference API support, please check:
|
| 20 |
https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 21 |
"""
|
| 22 |
|
| 23 |
# Initialize your Hugging Face model client
|
| 24 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 25 |
|
| 26 |
+
def respond(message, history: list[tuple[str, str]], system_message, max_tokens, temperature, top_p, file_content):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
"""
|
| 28 |
+
Streams the chat response from the Hugging Face model.
|
| 29 |
+
The uploaded file's content is appended to the system message context.
|
| 30 |
+
Yields tokens as they arrive.
|
| 31 |
"""
|
| 32 |
+
# Append file content to the system prompt if available.
|
| 33 |
+
if file_content and file_content.strip():
|
| 34 |
+
system_message = system_message + "\n\nFile content:\n" + file_content
|
| 35 |
|
| 36 |
+
# Build the messages list.
|
| 37 |
+
messages = [{"role": "system", "content": system_message}]
|
| 38 |
for val in history:
|
| 39 |
if val[0]:
|
| 40 |
messages.append({"role": "user", "content": val[0]})
|
| 41 |
if val[1]:
|
| 42 |
messages.append({"role": "assistant", "content": val[1]})
|
|
|
|
| 43 |
messages.append({"role": "user", "content": message})
|
| 44 |
|
|
|
|
| 45 |
response = ""
|
| 46 |
for partial in client.chat_completion(
|
| 47 |
messages,
|
|
|
|
| 55 |
yield response
|
| 56 |
|
| 57 |
###############################################################################
|
| 58 |
+
# File Upload & Parsing Functionality #
|
| 59 |
###############################################################################
|
| 60 |
|
| 61 |
+
def parse_file(file):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
"""
|
| 63 |
+
Parses the uploaded file.
|
| 64 |
+
For PDFs, it extracts text using PyPDF2.
|
| 65 |
+
For other file types, it attempts to decode as UTF-8 text.
|
| 66 |
"""
|
| 67 |
+
file_extension = file.name.split('.')[-1].lower()
|
| 68 |
+
if file_extension == "pdf":
|
| 69 |
+
try:
|
| 70 |
+
reader = PyPDF2.PdfReader(file)
|
| 71 |
+
text = ""
|
| 72 |
+
for page in reader.pages:
|
| 73 |
+
extracted = page.extract_text() or ""
|
| 74 |
+
text += extracted
|
| 75 |
+
return text
|
| 76 |
+
except Exception as e:
|
| 77 |
+
return f"Error reading PDF: {e}"
|
| 78 |
+
else:
|
| 79 |
+
try:
|
| 80 |
+
return file.read().decode("utf-8", errors="ignore")
|
| 81 |
+
except Exception as e:
|
| 82 |
+
return f"Error reading file: {e}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
+
def load_files(files):
|
| 85 |
"""
|
| 86 |
+
Processes a list of uploaded files.
|
| 87 |
+
Concatenates their text content.
|
| 88 |
"""
|
| 89 |
+
all_text = ""
|
| 90 |
+
for f in files:
|
| 91 |
+
content = parse_file(f)
|
| 92 |
+
all_text += content + "\n"
|
| 93 |
+
return all_text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
###############################################################################
|
| 96 |
+
# Gradio UI Layout #
|
| 97 |
###############################################################################
|
| 98 |
|
| 99 |
with gr.Blocks() as demo:
|
| 100 |
+
gr.Markdown("# Combined Chat & File Upload App")
|
| 101 |
gr.Markdown(
|
| 102 |
"""
|
| 103 |
+
This app allows you to upload file(s) and chat with an AI assistant that references the uploaded file(s) throughout the conversation.
|
| 104 |
+
- **Step 1:** Upload your file(s) (e.g., PDF or TXT).
|
| 105 |
+
- **Step 2:** Click **Load File(s)** to parse and store the file content.
|
| 106 |
+
- **Step 3:** Chat with the AI—the uploaded file's content will be appended to the context on every prompt.
|
| 107 |
"""
|
| 108 |
)
|
| 109 |
+
|
| 110 |
+
# Create a state to store the file's parsed content.
|
| 111 |
+
file_content_state = gr.State("")
|
| 112 |
+
|
| 113 |
+
with gr.Row():
|
| 114 |
+
file_input = gr.File(label="Upload File(s)", file_count="multiple")
|
| 115 |
+
load_button = gr.Button("Load File(s)")
|
| 116 |
+
|
| 117 |
+
# When the Load button is clicked, concatenate file contents into file_content_state.
|
| 118 |
+
load_button.click(fn=load_files, inputs=file_input, outputs=file_content_state)
|
| 119 |
+
|
| 120 |
+
gr.Markdown("## Chat with AI (using the uploaded file's content as context)")
|
| 121 |
+
# Note: We use Gradio’s ChatInterface which streams responses from the client.
|
| 122 |
+
demo_chat = gr.ChatInterface(
|
| 123 |
+
fn=respond,
|
| 124 |
+
additional_inputs=[
|
| 125 |
+
gr.Textbox(
|
| 126 |
+
value="You are a helpful AI assistant that uses the uploaded file's content as context.",
|
| 127 |
+
label="System message",
|
| 128 |
+
),
|
| 129 |
+
gr.Slider(
|
| 130 |
+
minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"
|
| 131 |
+
),
|
| 132 |
+
gr.Slider(
|
| 133 |
+
minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"
|
| 134 |
+
),
|
| 135 |
+
gr.Slider(
|
| 136 |
+
minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"
|
| 137 |
+
),
|
| 138 |
+
file_content_state, # The uploaded file's content is passed into each chat call.
|
| 139 |
+
],
|
| 140 |
+
)
|
| 141 |
+
|
| 142 |
+
demo.launch()
|
| 143 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
if __name__ == "__main__":
|
| 145 |
demo.launch()
|