Spaces:
Runtime error
Runtime error
Upload folder using huggingface_hub
Browse files- README.md +3 -9
- model.py +49 -0
- requirements.txt +4 -0
- templates/index.html +20 -0
README.md
CHANGED
|
@@ -1,12 +1,6 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
|
| 4 |
-
colorFrom: green
|
| 5 |
-
colorTo: green
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version: 4.
|
| 8 |
-
app_file: app.py
|
| 9 |
-
pinned: false
|
| 10 |
---
|
| 11 |
-
|
| 12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
---
|
| 2 |
+
title: bot_model
|
| 3 |
+
app_file: model.py
|
|
|
|
|
|
|
| 4 |
sdk: gradio
|
| 5 |
+
sdk_version: 4.21.0
|
|
|
|
|
|
|
| 6 |
---
|
|
|
|
|
|
model.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from pdfminer.high_level import extract_text
|
| 4 |
+
import openai
|
| 5 |
+
|
| 6 |
+
# Ensure your OPENAI_API_KEY environment variable is set
|
| 7 |
+
openai.api_key = "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"
|
| 8 |
+
|
| 9 |
+
def extract_text_from_pdf(pdf_file_path):
|
| 10 |
+
"""Extracts text from a PDF file."""
|
| 11 |
+
text = extract_text(pdf_file_path)
|
| 12 |
+
return text
|
| 13 |
+
|
| 14 |
+
def generate_response_with_openai(input_text):
|
| 15 |
+
"""Generates a response using OpenAI's GPT-3.5-turbo model based on the input text."""
|
| 16 |
+
response = openai.ChatCompletion.create(
|
| 17 |
+
model="gpt-3.5-turbo",
|
| 18 |
+
messages=[
|
| 19 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
| 20 |
+
{"role": "user", "content": input_text},
|
| 21 |
+
]
|
| 22 |
+
)
|
| 23 |
+
return response['choices'][0]['message']['content']
|
| 24 |
+
|
| 25 |
+
def chatbot(input_text, uploaded_file=None):
|
| 26 |
+
"""Process user input and an optional PDF file to generate a response."""
|
| 27 |
+
combined_text = input_text
|
| 28 |
+
|
| 29 |
+
if uploaded_file is not None:
|
| 30 |
+
# Extract text from the uploaded PDF file using its path
|
| 31 |
+
pdf_text = extract_text_from_pdf(uploaded_file)
|
| 32 |
+
combined_text += "\n\nExtracted PDF Text:\n" + pdf_text
|
| 33 |
+
|
| 34 |
+
response = generate_response_with_openai(combined_text)
|
| 35 |
+
return response
|
| 36 |
+
|
| 37 |
+
# Define the Gradio interface
|
| 38 |
+
iface = gr.Interface(fn=chatbot,
|
| 39 |
+
inputs=[
|
| 40 |
+
gr.Textbox(lines=7, placeholder="Enter your question here", label="Your Question"),
|
| 41 |
+
gr.File(label="Upload PDF File", type="filepath") # Adjusted to 'filepath'
|
| 42 |
+
],
|
| 43 |
+
outputs="text",
|
| 44 |
+
title="Custom Bot",
|
| 45 |
+
description="Interact with a custom model. You can also upload a PDF file for additional context.")
|
| 46 |
+
|
| 47 |
+
# Launch the Gradio interface with password protection
|
| 48 |
+
auth = [("naeem", "123naeem")] # Define username and password pairs
|
| 49 |
+
iface.launch(share=True, auth=auth)
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
pdfminer.six
|
| 3 |
+
openai==0.28
|
| 4 |
+
|
templates/index.html
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="en">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8">
|
| 5 |
+
<title>Custom Bot</title>
|
| 6 |
+
</head>
|
| 7 |
+
<body>
|
| 8 |
+
<h1>Custom Bot</h1>
|
| 9 |
+
<form method="post" enctype="multipart/form-data">
|
| 10 |
+
<label for="inputText">Your Question:</label><br>
|
| 11 |
+
<textarea id="inputText" name="inputText" rows="4" cols="50" placeholder="Enter your question here"></textarea><br>
|
| 12 |
+
<label for="pdfFile">Upload PDF File:</label>
|
| 13 |
+
<input type="file" id="pdfFile" name="pdfFile"><br><br>
|
| 14 |
+
<input type="submit" value="Submit">
|
| 15 |
+
</form>
|
| 16 |
+
{% if response %}
|
| 17 |
+
<div><strong>Response:</strong> {{ response }}</div>
|
| 18 |
+
{% endif %}
|
| 19 |
+
</body>
|
| 20 |
+
</html>
|