Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 2 |
+
import torch
|
| 3 |
+
|
| 4 |
+
# Load DistilGPT-2 (lightweight and fast)
|
| 5 |
+
distilgpt2_tokenizer = AutoTokenizer.from_pretrained("distilgpt2")
|
| 6 |
+
distilgpt2_model = AutoModelForCausalLM.from_pretrained("distilgpt2")
|
| 7 |
+
|
| 8 |
+
# Move model to GPU if available
|
| 9 |
+
if torch.cuda.is_available():
|
| 10 |
+
distilgpt2_model = distilgpt2_model.to("cuda")
|
| 11 |
+
|
| 12 |
+
def generate_response(prompt):
|
| 13 |
+
# Tokenize the input prompt
|
| 14 |
+
inputs = distilgpt2_tokenizer(prompt, return_tensors="pt").to(distilgpt2_model.device)
|
| 15 |
+
|
| 16 |
+
# Generate the response
|
| 17 |
+
outputs = distilgpt2_model.generate(**inputs, max_length=100)
|
| 18 |
+
|
| 19 |
+
# Decode the response
|
| 20 |
+
response = distilgpt2_tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 21 |
+
return response
|
| 22 |
+
|
| 23 |
+
import gradio as gr
|
| 24 |
+
|
| 25 |
+
# Gradio interface
|
| 26 |
+
def chatbot(prompt):
|
| 27 |
+
response = generate_response(prompt)
|
| 28 |
+
return response
|
| 29 |
+
|
| 30 |
+
interface = gr.Interface(
|
| 31 |
+
fn=chatbot,
|
| 32 |
+
inputs="text",
|
| 33 |
+
outputs="text",
|
| 34 |
+
title="DistilGPT-2 Chatbot",
|
| 35 |
+
description="Ask questions and get answers from DistilGPT-2!"
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
import fitz # PyMuPDF
|
| 39 |
+
import easyocr
|
| 40 |
+
from PIL import Image
|
| 41 |
+
|
| 42 |
+
# Function to extract text from PDF
|
| 43 |
+
def extract_text_from_pdf(pdf_path):
|
| 44 |
+
doc = fitz.open(pdf_path)
|
| 45 |
+
text = ""
|
| 46 |
+
for page in doc:
|
| 47 |
+
text += page.get_text()
|
| 48 |
+
return text
|
| 49 |
+
|
| 50 |
+
# Function to extract text from image
|
| 51 |
+
def extract_text_from_image(image_path):
|
| 52 |
+
reader = easyocr.Reader(['en'])
|
| 53 |
+
results = reader.readtext(image_path)
|
| 54 |
+
extracted_text = " ".join([res[1] for res in results])
|
| 55 |
+
return extracted_text
|
| 56 |
+
|
| 57 |
+
def chatbot(input_type, input_data):
|
| 58 |
+
if input_type == "Text":
|
| 59 |
+
prompt = input_data
|
| 60 |
+
elif input_type == "PDF":
|
| 61 |
+
pdf_text = extract_text_from_pdf(input_data)
|
| 62 |
+
prompt = f"Extracted text from PDF:\n{pdf_text}\n\nQuestion: {input_data}"
|
| 63 |
+
elif input_type == "Image":
|
| 64 |
+
image_text = extract_text_from_image(input_data)
|
| 65 |
+
prompt = f"Extracted text from image:\n{image_text}\n\nQuestion: {input_data}"
|
| 66 |
+
else:
|
| 67 |
+
return "Invalid input type."
|
| 68 |
+
|
| 69 |
+
# Generate response using the selected model
|
| 70 |
+
response = generate_response(prompt)
|
| 71 |
+
return response
|
| 72 |
+
|
| 73 |
+
# List of input types
|
| 74 |
+
INPUT_TYPES = ["Text", "PDF", "Image"]
|
| 75 |
+
|
| 76 |
+
# Gradio interface
|
| 77 |
+
interface = gr.Interface(
|
| 78 |
+
fn=chatbot,
|
| 79 |
+
inputs=[
|
| 80 |
+
gr.Dropdown(choices=INPUT_TYPES, label="Input Type"),
|
| 81 |
+
gr.Textbox(lines=2, placeholder="Enter text or upload a file...", label="Input")
|
| 82 |
+
],
|
| 83 |
+
outputs="text",
|
| 84 |
+
title="Lightweight Chatbot with PDF and Image Support",
|
| 85 |
+
description="Select the input type (Text, PDF, or Image) and ask your question!"
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
# Launch the app
|
| 89 |
+
interface.launch()
|