Delete app.py
Browse files
app.py
DELETED
@@ -1,129 +0,0 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
from huggingface_hub import InferenceClient
|
3 |
-
from PyPDF2 import PdfReader
|
4 |
-
|
5 |
-
# Initialize the Inference Client
|
6 |
-
client = InferenceClient("meta-llama/Llama-3.2-3B-Instruct")
|
7 |
-
|
8 |
-
|
9 |
-
def respond(
|
10 |
-
message,
|
11 |
-
history: list[tuple[str, str]],
|
12 |
-
system_message,
|
13 |
-
max_tokens,
|
14 |
-
temperature,
|
15 |
-
top_p,
|
16 |
-
uploaded_pdf=None
|
17 |
-
):
|
18 |
-
messages = [{"role": "system", "content": system_message}]
|
19 |
-
|
20 |
-
# Add previous conversation history to the messages
|
21 |
-
for val in history:
|
22 |
-
if val[0]:
|
23 |
-
messages.append({"role": "user", "content": val[0]})
|
24 |
-
if val[1]:
|
25 |
-
messages.append({"role": "assistant", "content": val[1]})
|
26 |
-
|
27 |
-
# If a new message is entered, add it to the conversation history
|
28 |
-
messages.append({"role": "user", "content": message})
|
29 |
-
|
30 |
-
# If a PDF is uploaded, process its content
|
31 |
-
if uploaded_pdf is not None:
|
32 |
-
file_content = extract_pdf_text(uploaded_pdf)
|
33 |
-
if file_content:
|
34 |
-
messages.append({"role": "user", "content": f"Document Content: {file_content}"})
|
35 |
-
|
36 |
-
# Get response from the model
|
37 |
-
response = ""
|
38 |
-
for message in client.chat_completion(
|
39 |
-
messages,
|
40 |
-
max_tokens=max_tokens,
|
41 |
-
stream=True,
|
42 |
-
temperature=temperature,
|
43 |
-
top_p=top_p,
|
44 |
-
):
|
45 |
-
token = message.choices[0].delta.content
|
46 |
-
response += token
|
47 |
-
yield response
|
48 |
-
|
49 |
-
|
50 |
-
def extract_pdf_text(file):
|
51 |
-
"""Extract text from a PDF file."""
|
52 |
-
try:
|
53 |
-
reader = PdfReader(file)
|
54 |
-
text = ""
|
55 |
-
for page in reader.pages:
|
56 |
-
text += page.extract_text()
|
57 |
-
return text
|
58 |
-
except Exception as e:
|
59 |
-
return f"Error extracting text from PDF: {str(e)}"
|
60 |
-
|
61 |
-
|
62 |
-
# CSS for styling the interface
|
63 |
-
css = """
|
64 |
-
body {
|
65 |
-
background-color: #1e2a38; /* Dark blue background */
|
66 |
-
color: #ffffff; /* White text for readability */
|
67 |
-
font-family: 'Arial', sans-serif; /* Clean and modern font */
|
68 |
-
}
|
69 |
-
.gr-button {
|
70 |
-
background-color: #42B3CE !important; /* Light blue button */
|
71 |
-
color: #2e3b4e !important; /* Dark text for contrast */
|
72 |
-
border: none !important;
|
73 |
-
padding: 10px 20px !important;
|
74 |
-
border-radius: 8px !important;
|
75 |
-
font-size: 16px;
|
76 |
-
font-weight: bold;
|
77 |
-
transition: background-color 0.3s ease, transform 0.2s ease;
|
78 |
-
}
|
79 |
-
.gr-button:hover {
|
80 |
-
background-color: #3189A2 !important; /* Darker blue on hover */
|
81 |
-
transform: scale(1.05);
|
82 |
-
}
|
83 |
-
.gr-button:active {
|
84 |
-
background-color: #267b88 !important; /* Even darker when clicked */
|
85 |
-
}
|
86 |
-
.gr-slider-container {
|
87 |
-
color: white !important; /* White slider labels */
|
88 |
-
font-size: 14px;
|
89 |
-
}
|
90 |
-
.gr-slider {
|
91 |
-
background-color: #0b2545 !important; /* Slider track color */
|
92 |
-
border-radius: 8px;
|
93 |
-
}
|
94 |
-
.gr-slider .gr-slider-active {
|
95 |
-
background-color: #42B3CE !important; /* Active slider color */
|
96 |
-
}
|
97 |
-
.gr-textbox input {
|
98 |
-
background-color: #2f3b4d;
|
99 |
-
color: white;
|
100 |
-
border: 2px solid #42B3CE;
|
101 |
-
padding: 12px;
|
102 |
-
border-radius: 8px;
|
103 |
-
font-size: 16px;
|
104 |
-
transition: border 0.3s ease;
|
105 |
-
}
|
106 |
-
.gr-textbox input:focus {
|
107 |
-
border-color: #3189A2;
|
108 |
-
}
|
109 |
-
"""
|
110 |
-
|
111 |
-
# Gradio interface
|
112 |
-
demo = gr.Interface(
|
113 |
-
fn=respond,
|
114 |
-
inputs=[
|
115 |
-
gr.Textbox(label="Your Message", placeholder="Type your question here...", lines=4),
|
116 |
-
gr.File(label="Upload a PDF", file_count="single", type="file"),
|
117 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens", visible=False),
|
118 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature", visible=False),
|
119 |
-
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)", visible=False),
|
120 |
-
],
|
121 |
-
outputs="text",
|
122 |
-
css=css, # Custom CSS
|
123 |
-
live=True,
|
124 |
-
title="Health Assistant Chat",
|
125 |
-
description="This is a health assistant that can chat with you about health-related topics. You can also upload a document for analysis.",
|
126 |
-
)
|
127 |
-
|
128 |
-
if __name__ == "__main__":
|
129 |
-
demo.launch(share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|