Upload folder using huggingface_hub
Browse files
test.py
CHANGED
@@ -1,96 +1,54 @@
|
|
1 |
import gradio as gr
|
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 |
-
max-width: 900px;
|
27 |
-
height: 90%;
|
28 |
-
box-shadow: 0 5px 15px rgba(0,0,0,0.3);
|
29 |
-
border-radius: 8px;
|
30 |
-
position: relative;
|
31 |
-
}
|
32 |
-
|
33 |
-
.close-btn {
|
34 |
-
position: absolute;
|
35 |
-
right: 15px;
|
36 |
-
top: 15px;
|
37 |
-
font-size: 24px;
|
38 |
-
cursor: pointer;
|
39 |
-
background: #222;
|
40 |
-
color: white;
|
41 |
-
border: none;
|
42 |
-
border-radius: 4px;
|
43 |
-
padding: 5px 15px;
|
44 |
-
z-index: 1001;
|
45 |
-
}
|
46 |
-
|
47 |
-
.pdf-container {
|
48 |
-
width: 100%;
|
49 |
-
height: calc(100% - 40px);
|
50 |
-
margin-top: 40px;
|
51 |
-
}
|
52 |
-
|
53 |
-
.pdf-embed {
|
54 |
-
width: 100%;
|
55 |
-
height: 100%;
|
56 |
-
border: 1px solid #ddd;
|
57 |
-
}
|
58 |
-
"""
|
59 |
-
|
60 |
-
modal_html = f"""
|
61 |
-
<div id="pdfModal" class="pdf-modal">
|
62 |
-
<div class="pdf-modal-content">
|
63 |
-
<button class="close-btn" onclick="document.getElementById('pdfModal').style.display='none';document.body.style.overflow='auto';">×</button>
|
64 |
-
<div class="pdf-container">
|
65 |
-
<iframe class="pdf-embed" src="{embed_url}" type="application/pdf"></iframe>
|
66 |
-
</div>
|
67 |
-
</div>
|
68 |
-
</div>
|
69 |
-
"""
|
70 |
-
|
71 |
-
with gr.Blocks(css=custom_css) as app:
|
72 |
-
gr.Markdown("## View Hate Speech Policy")
|
73 |
-
gr.HTML(modal_html)
|
74 |
-
|
75 |
-
open_button = gr.Button("📄 Open Policy Document")
|
76 |
-
|
77 |
-
open_button.click(
|
78 |
-
fn=lambda: None,
|
79 |
-
inputs=None,
|
80 |
-
outputs=None,
|
81 |
-
js="""
|
82 |
-
() => {
|
83 |
-
const modal = document.getElementById('pdfModal');
|
84 |
-
if (modal) {
|
85 |
-
modal.style.display = 'block';
|
86 |
-
document.body.style.overflow = 'hidden';
|
87 |
-
}
|
88 |
-
}
|
89 |
-
"""
|
90 |
)
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import openai
|
3 |
+
import os
|
4 |
+
import PyPDF2
|
5 |
+
|
6 |
+
# Read the API key from environment variable (you'll store it on Hugging Face Spaces)
|
7 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
8 |
+
|
9 |
+
def read_pdf(file):
|
10 |
+
if file is None:
|
11 |
+
return ""
|
12 |
+
reader = PyPDF2.PdfReader(file)
|
13 |
+
text = ""
|
14 |
+
for page in reader.pages:
|
15 |
+
text += page.extract_text()
|
16 |
+
return text
|
17 |
+
|
18 |
+
def chat_with_openai(user_input, model_name, uploaded_pdf):
|
19 |
+
pdf_text = read_pdf(uploaded_pdf) if uploaded_pdf else ""
|
20 |
+
prompt = f"{pdf_text}\n\nUser Query: {user_input}" if pdf_text else user_input
|
21 |
+
|
22 |
+
try:
|
23 |
+
response = openai.ChatCompletion.create(
|
24 |
+
model=model_name,
|
25 |
+
messages=[{"role": "user", "content": prompt}]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
)
|
27 |
+
return response['choices'][0]['message']['content']
|
28 |
+
except Exception as e:
|
29 |
+
return f"Error: {str(e)}"
|
30 |
+
|
31 |
+
with gr.Blocks() as app:
|
32 |
+
gr.Markdown("# 🔥 OpenAI Chat + PDF Analysis Tool")
|
33 |
+
with gr.Row():
|
34 |
+
model_selector = gr.Dropdown(
|
35 |
+
choices=["gpt-3.5-turbo", "gpt-4", "gpt-4-turbo"],
|
36 |
+
label="Select OpenAI Model",
|
37 |
+
value="gpt-3.5-turbo"
|
38 |
+
)
|
39 |
+
with gr.Row():
|
40 |
+
uploaded_pdf = gr.File(label="Upload a PDF (optional)", file_types=[".pdf"])
|
41 |
+
with gr.Row():
|
42 |
+
user_input = gr.Textbox(label="Your Prompt", placeholder="Ask anything...")
|
43 |
+
with gr.Row():
|
44 |
+
submit_btn = gr.Button("Submit")
|
45 |
+
with gr.Row():
|
46 |
+
response_output = gr.Textbox(label="OpenAI Response", lines=10, interactive=True)
|
47 |
+
|
48 |
+
submit_btn.click(
|
49 |
+
fn=chat_with_openai,
|
50 |
+
inputs=[user_input, model_selector, uploaded_pdf],
|
51 |
+
outputs=response_output
|
52 |
+
)
|
53 |
+
|
54 |
+
app.launch()
|