gradio_app / test_gradio.py
dupuyta's picture
Upload folder using huggingface_hub
8e2b48f
# in folder ==> gradio test_gradio.py ( it won’t provide the automatic reload mechanism?)
import gradio as gr
import numpy as np
import random
import time
# def greet(name):
# return "Hello " + name + "!"
# demo = gr.Interface(fn=greet, inputs=gr.Textbox(lines=2, placeholder="Name Here..."), outputs="text")
# demo.launch()
# def greet2(name, is_morning, temperature):
# salutation = "Good morning" if is_morning else "Good evening"
# greeting = f"{salutation} {name}. It is {temperature} degrees today"
# celsius = (temperature - 32) * 5 / 9
# return greeting, round(celsius, 2)
# demo = gr.Interface(
# fn=greet2,
# inputs=["text", "checkbox", gr.Slider(0, 100)],
# outputs=["text", "number"],
# )
# demo.launch()
# def sepia(input_img):
# sepia_filter = np.array([
# [0.393, 0.769, 0.189],
# [0.349, 0.686, 0.168],
# [0.272, 0.534, 0.131]
# ])
# sepia_img = input_img.dot(sepia_filter.T)
# sepia_img /= sepia_img.max()
# return sepia_img
# demo = gr.Interface(sepia, gr.Image(shape=(200, 200)), "image")
# demo.launch()
def yes_man(message, history):
if message.endswith("?"):
return "Yes"
else:
return "Ask me anything!"
# gr.ChatInterface(
# yes_man,
# chatbot=gr.Chatbot(height=300),
# textbox=gr.Textbox(placeholder="Ask a question about the uploaded PDF document.", container=False, scale=7),
# title="Gradio QA Bot",
# description=f"{intro}",
# theme="soft",
# examples=["What is the title of the document?", "Summarize the main ideas of the documents"],
# cache_examples=True,
# retry_btn=None,
# undo_btn="Delete Previous",
# clear_btn="Clear",
# ).launch()
# intro = "Welcome! This is not just any bot, ..."
title1 = "QA App"
title2 = "Gradio QA Bot"
def file_upload(input_file):
# Process the uploaded file
if input_file is not None:
# Save the uploaded file or perform any desired operations
file_path = "/tmp/file.pdf"
content = input_file.read()
try:
with open(file_path, 'wb') as file:
file.write(content)
return [f"File '{input_file.name}' uploaded successfully in {file_path}.",file_path]
except Exception as e:
return f"Error occurred while writing the file: {e}"
return ["No file uploaded.", file_path]
def crash(test, file):
return("ok")
gr.ChatInterface(
yes_man,
chatbot=gr.Chatbot(height=300),
textbox=gr.Textbox(placeholder="Ask a question about the uploaded PDF document.", container=False, scale=7),
title="Gradio QA Bot",
description="blabla",
theme="soft",
examples=["What is the title of the document?", "Summarize the main ideas of the documents"],
cache_examples=True,
retry_btn=None,
undo_btn="Delete Previous",
clear_btn="Clear",
).launch()
# with gr.Blocks() as demo:
# intro = gr.Markdown("""Welcome! This is not just any bot, it's a special one equipped with state-of-the-art natural language processing capabilities, and ready to answer your queries.
# Ready to explore? Let's get started!
# * Step 1: Upload a PDF document.
# * Step 2: Type in a question related to your document's content.
# * Step 3: Get your answer!
# Push clear cache before uploading a new doc!
# """)
# # Create a Gradio interface with a file upload input
# iface = gr.Interface(
# fn=file_upload,
# inputs=gr.File(),
# outputs=["text", gr.File()],
# title=title1,
# description="Drag and drop your document here")
# # bot = gr.Interface(crash,
# # inputs=[gr.Textbox(lines=2, placeholder="Ask a question about the uploaded PDF document."), gr.File()],
# # outputs=[gr.Chatbot(height=300)],
# # title="Gradio QA Bot",
# # description=f"{intro}",
# # theme="soft",
# # examples=["What is the title of the document?", "Summarize the main ideas of the documents"],
# # cache_examples=True,
# # retry_btn=None,
# # undo_btn="Delete Previous",
# # clear_btn="Clear")
# # gr.ChatInterface(
# # yes_man,
# # chatbot=gr.Chatbot(height=300),
# # textbox=gr.Textbox(placeholder="Ask a question about the uploaded PDF document.", container=False, scale=7),
# # title="Gradio QA Bot",
# # description=f"{intro}",
# # theme="soft",
# # examples=["What is the title of the document?", "Summarize the main ideas of the documents"],
# # cache_examples=True,
# # retry_btn=None,
# # undo_btn="Delete Previous",
# # clear_btn="Clear",
# # )
# demo.launch()
# bot
iface = gr.Interface(qa_bot,
inputs=["file", gr.Textbox(placeholder="Ask a question about the uploaded PDF document.", container=False, scale=7)],
outputs="text",
title=title2,
description="Ask a question about the uploaded PDF document.",
theme="soft",
examples=["What is the title of the document?", "Summarize the main ideas of the documents"],
cache_examples=True,
retry_btn=None,
undo_btn="Delete Previous",
clear_btn="Clear")
#### OR
iface = gr.ChatInterface(
qa_bot,
chatbot=gr.Chatbot(height=300),
textbox=gr.Textbox(placeholder="Ask a question about the uploaded PDF document.", container=False, scale=7),
title=title2,
description="Ask a question about the uploaded PDF document.",
theme="soft",
examples=["What is the title of the document?", "Summarize the main ideas of the documents"],
cache_examples=True,
retry_btn=None,
undo_btn="Delete Previous",
clear_btn="Clear",
)