Spaces:
Runtime error
Runtime error
File size: 6,163 Bytes
8e2b48f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# 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",
) |