Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,10 +3,10 @@ from huggingface_hub import InferenceClient
|
|
| 3 |
import nltk
|
| 4 |
import PyPDF2
|
| 5 |
|
| 6 |
-
# Download necessary NLTK data
|
| 7 |
nltk.download("punkt", quiet=True)
|
| 8 |
|
| 9 |
-
# Hugging Face Inference Client
|
| 10 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 11 |
|
| 12 |
# Function to split text into manageable chunks
|
|
@@ -31,7 +31,7 @@ def chunk_text(text, max_chunk_size=1500):
|
|
| 31 |
chunks.append(current_chunk.strip())
|
| 32 |
return chunks
|
| 33 |
|
| 34 |
-
#
|
| 35 |
def respond_chunked(message, history, system_message, max_tokens, temperature, top_p, file_content):
|
| 36 |
if not file_content.strip():
|
| 37 |
return "No file content available to provide context."
|
|
@@ -54,7 +54,7 @@ def respond_chunked(message, history, system_message, max_tokens, temperature, t
|
|
| 54 |
combined_response += f"Error processing chunk: {e}\n"
|
| 55 |
return combined_response.strip()
|
| 56 |
|
| 57 |
-
#
|
| 58 |
def parse_file(file_obj):
|
| 59 |
file_extension = file_obj.name.split('.')[-1].lower()
|
| 60 |
if file_extension == "pdf":
|
|
@@ -69,21 +69,21 @@ def parse_file(file_obj):
|
|
| 69 |
except Exception as e:
|
| 70 |
return f"Error reading file: {e}"
|
| 71 |
|
| 72 |
-
# Define the Gradio app
|
| 73 |
with gr.Blocks() as demo:
|
| 74 |
gr.Markdown("# **Chat with File Context (Chunking for Large Files)**")
|
| 75 |
gr.Markdown("Upload large files, and chat with AI using context derived from those files.")
|
| 76 |
|
| 77 |
-
#
|
| 78 |
file_content_state = gr.State("")
|
| 79 |
chat_history_state = gr.State([])
|
| 80 |
|
| 81 |
-
# File upload
|
| 82 |
file_input = gr.File(label="Upload File(s)", file_count="multiple", type="filepath")
|
| 83 |
|
| 84 |
def handle_file_upload(files):
|
| 85 |
"""
|
| 86 |
-
Process uploaded files and store their content
|
| 87 |
"""
|
| 88 |
combined_text = ""
|
| 89 |
for file in files:
|
|
@@ -97,10 +97,9 @@ with gr.Blocks() as demo:
|
|
| 97 |
|
| 98 |
file_input.change(fn=handle_file_upload, inputs=file_input, outputs=file_content_state)
|
| 99 |
|
| 100 |
-
# Chat interface
|
| 101 |
chatbot = gr.Chatbot(label="Conversation", type="messages")
|
| 102 |
user_input = gr.Textbox(label="Your Message", placeholder="Ask something...", lines=2)
|
| 103 |
-
|
| 104 |
system_prompt = gr.Textbox(label="System Prompt", value="You are a helpful AI assistant.", interactive=True)
|
| 105 |
max_tokens = gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max Tokens")
|
| 106 |
temperature = gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature")
|
|
@@ -109,15 +108,16 @@ with gr.Blocks() as demo:
|
|
| 109 |
def chat_function(user_message, history, file_content, system_prompt, max_tokens, temperature, top_p):
|
| 110 |
if not user_message.strip():
|
| 111 |
return "", history
|
|
|
|
| 112 |
assistant_response = respond_chunked(
|
| 113 |
user_message, history, system_prompt, max_tokens, temperature, top_p, file_content
|
| 114 |
)
|
| 115 |
-
# Append user and assistant messages
|
| 116 |
history.append({"role": "user", "content": user_message})
|
| 117 |
history.append({"role": "assistant", "content": assistant_response})
|
| 118 |
return "", history
|
| 119 |
|
| 120 |
-
#
|
| 121 |
send_button = gr.Button("Send")
|
| 122 |
send_button.click(
|
| 123 |
fn=chat_function,
|
|
@@ -125,6 +125,7 @@ with gr.Blocks() as demo:
|
|
| 125 |
outputs=[user_input, chatbot]
|
| 126 |
)
|
| 127 |
|
|
|
|
| 128 |
user_input.submit(
|
| 129 |
fn=chat_function,
|
| 130 |
inputs=[user_input, chat_history_state, file_content_state, system_prompt, max_tokens, temperature, top_p],
|
|
@@ -132,4 +133,3 @@ with gr.Blocks() as demo:
|
|
| 132 |
)
|
| 133 |
|
| 134 |
demo.launch(server_name="0.0.0.0", server_port=7860, share=True, show_error=True)
|
| 135 |
-
|
|
|
|
| 3 |
import nltk
|
| 4 |
import PyPDF2
|
| 5 |
|
| 6 |
+
# Download the necessary NLTK data (using the correct resource "punkt")
|
| 7 |
nltk.download("punkt", quiet=True)
|
| 8 |
|
| 9 |
+
# Initialize the Hugging Face Inference Client
|
| 10 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 11 |
|
| 12 |
# Function to split text into manageable chunks
|
|
|
|
| 31 |
chunks.append(current_chunk.strip())
|
| 32 |
return chunks
|
| 33 |
|
| 34 |
+
# Function to provide responses for each text chunk
|
| 35 |
def respond_chunked(message, history, system_message, max_tokens, temperature, top_p, file_content):
|
| 36 |
if not file_content.strip():
|
| 37 |
return "No file content available to provide context."
|
|
|
|
| 54 |
combined_response += f"Error processing chunk: {e}\n"
|
| 55 |
return combined_response.strip()
|
| 56 |
|
| 57 |
+
# Function to parse the uploaded file based on its extension
|
| 58 |
def parse_file(file_obj):
|
| 59 |
file_extension = file_obj.name.split('.')[-1].lower()
|
| 60 |
if file_extension == "pdf":
|
|
|
|
| 69 |
except Exception as e:
|
| 70 |
return f"Error reading file: {e}"
|
| 71 |
|
| 72 |
+
# Define the Gradio app interface
|
| 73 |
with gr.Blocks() as demo:
|
| 74 |
gr.Markdown("# **Chat with File Context (Chunking for Large Files)**")
|
| 75 |
gr.Markdown("Upload large files, and chat with AI using context derived from those files.")
|
| 76 |
|
| 77 |
+
# States to store file content and chat history
|
| 78 |
file_content_state = gr.State("")
|
| 79 |
chat_history_state = gr.State([])
|
| 80 |
|
| 81 |
+
# File upload component (accepts multiple files)
|
| 82 |
file_input = gr.File(label="Upload File(s)", file_count="multiple", type="filepath")
|
| 83 |
|
| 84 |
def handle_file_upload(files):
|
| 85 |
"""
|
| 86 |
+
Process uploaded files and store their content.
|
| 87 |
"""
|
| 88 |
combined_text = ""
|
| 89 |
for file in files:
|
|
|
|
| 97 |
|
| 98 |
file_input.change(fn=handle_file_upload, inputs=file_input, outputs=file_content_state)
|
| 99 |
|
| 100 |
+
# Chat interface components
|
| 101 |
chatbot = gr.Chatbot(label="Conversation", type="messages")
|
| 102 |
user_input = gr.Textbox(label="Your Message", placeholder="Ask something...", lines=2)
|
|
|
|
| 103 |
system_prompt = gr.Textbox(label="System Prompt", value="You are a helpful AI assistant.", interactive=True)
|
| 104 |
max_tokens = gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max Tokens")
|
| 105 |
temperature = gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature")
|
|
|
|
| 108 |
def chat_function(user_message, history, file_content, system_prompt, max_tokens, temperature, top_p):
|
| 109 |
if not user_message.strip():
|
| 110 |
return "", history
|
| 111 |
+
# Get the assistant's response using the chunking function
|
| 112 |
assistant_response = respond_chunked(
|
| 113 |
user_message, history, system_prompt, max_tokens, temperature, top_p, file_content
|
| 114 |
)
|
| 115 |
+
# Append user and assistant messages to the conversation history
|
| 116 |
history.append({"role": "user", "content": user_message})
|
| 117 |
history.append({"role": "assistant", "content": assistant_response})
|
| 118 |
return "", history
|
| 119 |
|
| 120 |
+
# Button to send the user message
|
| 121 |
send_button = gr.Button("Send")
|
| 122 |
send_button.click(
|
| 123 |
fn=chat_function,
|
|
|
|
| 125 |
outputs=[user_input, chatbot]
|
| 126 |
)
|
| 127 |
|
| 128 |
+
# Enable submission via the Enter key in the textbox
|
| 129 |
user_input.submit(
|
| 130 |
fn=chat_function,
|
| 131 |
inputs=[user_input, chat_history_state, file_content_state, system_prompt, max_tokens, temperature, top_p],
|
|
|
|
| 133 |
)
|
| 134 |
|
| 135 |
demo.launch(server_name="0.0.0.0", server_port=7860, share=True, show_error=True)
|
|
|