custom-ai-bot / model.py
iamnesar's picture
Upload folder using huggingface_hub
b1889e5 verified
Raw History Blame Contribute Delete
1.94 kB
import os
import gradio as gr
from pdfminer.high_level import extract_text
import openai
# Ensure your OPENAI_API_KEY environment variable is set
openai.api_key = "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"
def extract_text_from_pdf(pdf_file_path):
"""Extracts text from a PDF file."""
text = extract_text(pdf_file_path)
return text
def generate_response_with_openai(input_text):
"""Generates a response using OpenAI's GPT-3.5-turbo model based on the input text."""
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": input_text},
]
)
return response['choices'][0]['message']['content']
def chatbot(input_text, uploaded_file=None):
"""Process user input and an optional PDF file to generate a response."""
combined_text = input_text
if uploaded_file is not None:
# Extract text from the uploaded PDF file using its path
pdf_text = extract_text_from_pdf(uploaded_file)
combined_text += "\n\nExtracted PDF Text:\n" + pdf_text
response = generate_response_with_openai(combined_text)
return response
# Define the Gradio interface
iface = gr.Interface(fn=chatbot,
inputs=[
gr.Textbox(lines=7, placeholder="Enter your question here", label="Your Question"),
gr.File(label="Upload PDF File", type="filepath") # Adjusted to 'filepath'
],
outputs="text",
title="Custom Bot",
description="Interact with a custom model. You can also upload a PDF file for additional context.")
# Launch the Gradio interface with password protection
auth = [("naeem", "123naeem")] # Define username and password pairs
iface.launch(share=True, auth=auth)