Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,23 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
-
|
| 5 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 6 |
-
"""
|
| 7 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
def respond(
|
| 11 |
message,
|
| 12 |
history: list[tuple[str, str]],
|
|
@@ -39,25 +50,25 @@ def respond(
|
|
| 39 |
response += token
|
| 40 |
yield response
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
demo = gr.ChatInterface(
|
| 46 |
-
respond,
|
| 47 |
additional_inputs=[
|
| 48 |
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 49 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 50 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 51 |
-
gr.Slider(
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
value=0.95,
|
| 55 |
-
step=0.05,
|
| 56 |
-
label="Top-p (nucleus sampling)",
|
| 57 |
-
),
|
| 58 |
-
],
|
| 59 |
)
|
| 60 |
|
| 61 |
-
|
| 62 |
if __name__ == "__main__":
|
| 63 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
+
# Existing client setup for chat model
|
|
|
|
|
|
|
| 5 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 6 |
|
| 7 |
+
# Function to process file uploads (e.g., text or PDF files)
|
| 8 |
+
def analyze_file(file):
|
| 9 |
+
if file is None:
|
| 10 |
+
return "Please upload a file."
|
| 11 |
|
| 12 |
+
# Assuming a text file for simplicity, modify for other types as needed (e.g., PDF)
|
| 13 |
+
try:
|
| 14 |
+
with open(file.name, "r", encoding="utf-8") as f:
|
| 15 |
+
content = f.read()
|
| 16 |
+
return content
|
| 17 |
+
except Exception as e:
|
| 18 |
+
return f"Error reading file: {str(e)}"
|
| 19 |
+
|
| 20 |
+
# Existing chatbot response function
|
| 21 |
def respond(
|
| 22 |
message,
|
| 23 |
history: list[tuple[str, str]],
|
|
|
|
| 50 |
response += token
|
| 51 |
yield response
|
| 52 |
|
| 53 |
+
# Adding a new function that uses file content as input to the chatbot
|
| 54 |
+
def respond_with_file(file, history, system_message, max_tokens, temperature, top_p):
|
| 55 |
+
file_content = analyze_file(file) # Get the content from the file
|
| 56 |
+
if isinstance(file_content, str):
|
| 57 |
+
return respond(file_content, history, system_message, max_tokens, temperature, top_p)
|
| 58 |
+
else:
|
| 59 |
+
return file_content
|
| 60 |
+
|
| 61 |
+
# Gradio interface setup
|
| 62 |
demo = gr.ChatInterface(
|
| 63 |
+
respond, # Main chat function
|
| 64 |
additional_inputs=[
|
| 65 |
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 66 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 67 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 68 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
| 69 |
+
gr.File(file_types=["text"], label="Upload a file (optional)"),
|
| 70 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
)
|
| 72 |
|
|
|
|
| 73 |
if __name__ == "__main__":
|
| 74 |
+
demo.launch()
|