Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -20,31 +20,30 @@ 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
|
| 24 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 25 |
|
| 26 |
-
def respond(message, history: list[
|
| 27 |
"""
|
| 28 |
Calls the model (in non-streaming mode) to get a complete response.
|
| 29 |
-
|
|
|
|
|
|
|
| 30 |
"""
|
| 31 |
if file_content and file_content.strip():
|
| 32 |
system_message += "\n\nFile content:\n" + file_content
|
| 33 |
|
| 34 |
-
# Build messages list
|
| 35 |
messages = [{"role": "system", "content": system_message}]
|
| 36 |
-
for
|
| 37 |
-
|
| 38 |
-
messages.append({"role": "user", "content": user_msg})
|
| 39 |
-
if assistant_msg:
|
| 40 |
-
messages.append({"role": "assistant", "content": assistant_msg})
|
| 41 |
messages.append({"role": "user", "content": message})
|
| 42 |
|
| 43 |
try:
|
| 44 |
completion = client.chat_completion(
|
| 45 |
messages,
|
| 46 |
max_tokens=max_tokens,
|
| 47 |
-
stream=False, # Non-streaming mode for simplicity
|
| 48 |
temperature=temperature,
|
| 49 |
top_p=top_p,
|
| 50 |
)
|
|
@@ -54,7 +53,7 @@ def respond(message, history: list[tuple[str, str]], system_message, max_tokens,
|
|
| 54 |
return response
|
| 55 |
|
| 56 |
###############################################################################
|
| 57 |
-
#
|
| 58 |
###############################################################################
|
| 59 |
|
| 60 |
def parse_file(file_obj):
|
|
@@ -98,31 +97,31 @@ def load_files(files):
|
|
| 98 |
###############################################################################
|
| 99 |
|
| 100 |
with gr.Blocks() as demo:
|
| 101 |
-
gr.Markdown("# Combined Chat & File Upload App")
|
| 102 |
gr.Markdown(
|
| 103 |
"""
|
| 104 |
-
This app allows you to upload file(s) (PDF or TXT)
|
| 105 |
-
|
| 106 |
-
|
|
|
|
| 107 |
"""
|
| 108 |
)
|
| 109 |
|
| 110 |
-
# State to hold
|
| 111 |
file_content_state = gr.State("")
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
#
|
| 116 |
-
# Using type="filepath" so that we get a file path that can be opened later.
|
| 117 |
file_input = gr.File(label="Upload File(s)", file_count="multiple", type="filepath")
|
| 118 |
-
# Automatically process files
|
| 119 |
file_input.change(fn=load_files, inputs=file_input, outputs=file_content_state)
|
| 120 |
|
| 121 |
gr.Markdown("## Chat")
|
| 122 |
-
chatbot = gr.Chatbot(label="Chat History")
|
| 123 |
user_input = gr.Textbox(label="Your Message", placeholder="Type your message here...", lines=2)
|
| 124 |
|
| 125 |
-
# Additional model parameters
|
| 126 |
system_prompt = gr.Textbox(label="System Message", value="You are a helpful AI assistant.", interactive=True)
|
| 127 |
max_tokens_slider = gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max New Tokens")
|
| 128 |
temperature_slider = gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature")
|
|
@@ -131,15 +130,15 @@ Simply upload a file and then start chatting.
|
|
| 131 |
def chat_fn(user_msg, history, file_content, system_msg, max_tokens, temperature, top_p):
|
| 132 |
if not user_msg.strip():
|
| 133 |
return "", history
|
| 134 |
-
# Append
|
| 135 |
-
history.append(
|
| 136 |
-
#
|
| 137 |
response = respond(user_msg, history, system_msg, max_tokens, temperature, top_p, file_content)
|
| 138 |
-
#
|
| 139 |
-
history
|
| 140 |
return "", history
|
| 141 |
|
| 142 |
-
#
|
| 143 |
user_input.submit(
|
| 144 |
fn=chat_fn,
|
| 145 |
inputs=[user_input, chat_history_state, file_content_state, system_prompt, max_tokens_slider, temperature_slider, top_p_slider],
|
|
@@ -147,7 +146,16 @@ Simply upload a file and then start chatting.
|
|
| 147 |
queue=True
|
| 148 |
)
|
| 149 |
|
| 150 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
|
| 152 |
if __name__ == "__main__":
|
| 153 |
demo.launch()
|
|
|
|
| 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[dict], system_message, max_tokens, temperature, top_p, file_content):
|
| 27 |
"""
|
| 28 |
Calls the model (in non-streaming mode) to get a complete response.
|
| 29 |
+
The file content is appended to the system message as context.
|
| 30 |
+
Expects conversation history in the format:
|
| 31 |
+
[{"role": "user", "content": ...}, {"role": "assistant", "content": ...}, ...]
|
| 32 |
"""
|
| 33 |
if file_content and file_content.strip():
|
| 34 |
system_message += "\n\nFile content:\n" + file_content
|
| 35 |
|
| 36 |
+
# Build messages list for the API request.
|
| 37 |
messages = [{"role": "system", "content": system_message}]
|
| 38 |
+
for entry in history:
|
| 39 |
+
messages.append(entry)
|
|
|
|
|
|
|
|
|
|
| 40 |
messages.append({"role": "user", "content": message})
|
| 41 |
|
| 42 |
try:
|
| 43 |
completion = client.chat_completion(
|
| 44 |
messages,
|
| 45 |
max_tokens=max_tokens,
|
| 46 |
+
stream=False, # Non-streaming mode for simplicity.
|
| 47 |
temperature=temperature,
|
| 48 |
top_p=top_p,
|
| 49 |
)
|
|
|
|
| 53 |
return response
|
| 54 |
|
| 55 |
###############################################################################
|
| 56 |
+
# File Upload & Parsing Functions #
|
| 57 |
###############################################################################
|
| 58 |
|
| 59 |
def parse_file(file_obj):
|
|
|
|
| 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) (e.g., PDF or TXT) and chat with an AI assistant that uses the uploaded file(s) for context throughout the conversation.
|
| 104 |
+
|
| 105 |
+
- **Upload File(s):** The file contents are automatically parsed and stored.
|
| 106 |
+
- **Chat:** Your message, along with the uploaded file content, is sent to the AI on every prompt.
|
| 107 |
"""
|
| 108 |
)
|
| 109 |
|
| 110 |
+
# State to hold the concatenated file content and conversation history.
|
| 111 |
file_content_state = gr.State("")
|
| 112 |
+
chat_history_state = gr.State([]) # List of dictionaries in the form {"role": "user"/"assistant", "content": ...}
|
| 113 |
+
|
| 114 |
+
# --- File Upload Section ---
|
| 115 |
+
# Use type="filepath" so that we get file paths for processing.
|
|
|
|
| 116 |
file_input = gr.File(label="Upload File(s)", file_count="multiple", type="filepath")
|
| 117 |
+
# Automatically process files upon upload.
|
| 118 |
file_input.change(fn=load_files, inputs=file_input, outputs=file_content_state)
|
| 119 |
|
| 120 |
gr.Markdown("## Chat")
|
| 121 |
+
chatbot = gr.Chatbot(label="Chat History", type="messages")
|
| 122 |
user_input = gr.Textbox(label="Your Message", placeholder="Type your message here...", lines=2)
|
| 123 |
|
| 124 |
+
# Additional model parameters (adjustable)
|
| 125 |
system_prompt = gr.Textbox(label="System Message", value="You are a helpful AI assistant.", interactive=True)
|
| 126 |
max_tokens_slider = gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max New Tokens")
|
| 127 |
temperature_slider = gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature")
|
|
|
|
| 130 |
def chat_fn(user_msg, history, file_content, system_msg, max_tokens, temperature, top_p):
|
| 131 |
if not user_msg.strip():
|
| 132 |
return "", history
|
| 133 |
+
# Append user's message (in the required format).
|
| 134 |
+
history.append({"role": "user", "content": user_msg})
|
| 135 |
+
# Get the AI's response.
|
| 136 |
response = respond(user_msg, history, system_msg, max_tokens, temperature, top_p, file_content)
|
| 137 |
+
# Append the assistant's response.
|
| 138 |
+
history.append({"role": "assistant", "content": response})
|
| 139 |
return "", history
|
| 140 |
|
| 141 |
+
# Trigger sending message on Enter in the textbox.
|
| 142 |
user_input.submit(
|
| 143 |
fn=chat_fn,
|
| 144 |
inputs=[user_input, chat_history_state, file_content_state, system_prompt, max_tokens_slider, temperature_slider, top_p_slider],
|
|
|
|
| 146 |
queue=True
|
| 147 |
)
|
| 148 |
|
| 149 |
+
# Also add a "Send" button.
|
| 150 |
+
send_button = gr.Button("Send")
|
| 151 |
+
send_button.click(
|
| 152 |
+
fn=chat_fn,
|
| 153 |
+
inputs=[user_input, chat_history_state, file_content_state, system_prompt, max_tokens_slider, temperature_slider, top_p_slider],
|
| 154 |
+
outputs=[user_input, chatbot],
|
| 155 |
+
queue=True
|
| 156 |
+
)
|
| 157 |
+
|
| 158 |
+
demo.launch(server_name="0.0.0.0", server_port=7860, share=True)
|
| 159 |
|
| 160 |
if __name__ == "__main__":
|
| 161 |
demo.launch()
|