Spaces:
Running
Running
Delete app.py
Browse files
app.py
DELETED
@@ -1,130 +0,0 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import spaces
|
3 |
-
import PyPDF2
|
4 |
-
import docx
|
5 |
-
import io
|
6 |
-
import os
|
7 |
-
from typing import Optional
|
8 |
-
from huggingface_hub import InferenceClient
|
9 |
-
from prompts import SYSTEM_PROMPT, PROMPTS
|
10 |
-
|
11 |
-
def extract_text_from_file(file) -> str:
|
12 |
-
"""Extract text from uploaded files"""
|
13 |
-
if file is None:
|
14 |
-
return ""
|
15 |
-
|
16 |
-
file_path = file.name
|
17 |
-
text = ""
|
18 |
-
|
19 |
-
try:
|
20 |
-
if file_path.endswith('.pdf'):
|
21 |
-
with open(file_path, 'rb') as f:
|
22 |
-
reader = PyPDF2.PdfReader(f)
|
23 |
-
for page in reader.pages:
|
24 |
-
text += page.extract_text() + "\n"
|
25 |
-
|
26 |
-
elif file_path.endswith('.docx'):
|
27 |
-
doc = docx.Document(file_path)
|
28 |
-
for paragraph in doc.paragraphs:
|
29 |
-
text += paragraph.text + "\n"
|
30 |
-
|
31 |
-
elif file_path.endswith('.txt'):
|
32 |
-
with open(file_path, 'r', encoding='utf-8') as f:
|
33 |
-
text = f.read()
|
34 |
-
except Exception as e:
|
35 |
-
return f"Error reading file: {str(e)}"
|
36 |
-
|
37 |
-
return text
|
38 |
-
|
39 |
-
@spaces.GPU
|
40 |
-
def process_document(document, operation_type, text_input):
|
41 |
-
"""Main processing function using Cerebras Llama through HuggingFace"""
|
42 |
-
|
43 |
-
# Extract text from file or use text input
|
44 |
-
if document is not None:
|
45 |
-
text = extract_text_from_file(document)
|
46 |
-
else:
|
47 |
-
text = text_input
|
48 |
-
|
49 |
-
if not text.strip():
|
50 |
-
return "Please provide either a document or text input."
|
51 |
-
|
52 |
-
# Get the appropriate prompt
|
53 |
-
prompt = PROMPTS.get(operation_type, "")
|
54 |
-
|
55 |
-
# Create the client with Cerebras provider
|
56 |
-
try:
|
57 |
-
client = InferenceClient(
|
58 |
-
"meta-llama/Llama-3.3-70B-Instruct",
|
59 |
-
provider="cerebras",
|
60 |
-
token=os.getenv("HF_TOKEN"),
|
61 |
-
)
|
62 |
-
|
63 |
-
# Create conversation messages
|
64 |
-
messages = [
|
65 |
-
{"role": "system", "content": SYSTEM_PROMPT},
|
66 |
-
{"role": "user", "content": f"{prompt}\n\nDocument content:\n{text}"}
|
67 |
-
]
|
68 |
-
|
69 |
-
# Generate response using chat completion
|
70 |
-
response = client.chat_completion(
|
71 |
-
messages=messages,
|
72 |
-
max_tokens=3000,
|
73 |
-
temperature=0.1,
|
74 |
-
stream=False
|
75 |
-
)
|
76 |
-
|
77 |
-
return response.choices[0].message.content
|
78 |
-
|
79 |
-
except Exception as e:
|
80 |
-
return f"Error: {str(e)}\n\nPlease ensure:\n1. HF_TOKEN is set in settings\n2. You have Pro access to use Cerebras inference\n3. The Cerebras/Llama integration is enabled in your account"
|
81 |
-
|
82 |
-
# Create the Gradio interface
|
83 |
-
with gr.Blocks(title="Study Assistant", theme=gr.themes.Soft()) as demo:
|
84 |
-
gr.Markdown("# 📚 Study Assistant - Document Analysis Tool")
|
85 |
-
gr.Markdown("Upload a document or paste text, then select the type of analysis you want to perform.")
|
86 |
-
gr.Markdown("*Powered by Meta Llama-3.3-70B via Cerebras on HuggingFace*")
|
87 |
-
|
88 |
-
with gr.Row():
|
89 |
-
with gr.Column():
|
90 |
-
document = gr.File(
|
91 |
-
label="Upload Document",
|
92 |
-
file_types=[".pdf", ".docx", ".txt"],
|
93 |
-
file_count="single"
|
94 |
-
)
|
95 |
-
text_input = gr.Textbox(
|
96 |
-
label="Or paste text directly",
|
97 |
-
lines=5,
|
98 |
-
placeholder="Paste your text here if you don't want to upload a file..."
|
99 |
-
)
|
100 |
-
|
101 |
-
with gr.Column():
|
102 |
-
operation_type = gr.Dropdown(
|
103 |
-
choices=["Summary", "Outline", "Analysis", "Study Guide", "Table", "Questions"],
|
104 |
-
label="Select Operation",
|
105 |
-
value="Summary"
|
106 |
-
)
|
107 |
-
process_btn = gr.Button("🚀 Process Document", variant="primary", size="lg")
|
108 |
-
|
109 |
-
output = gr.Textbox(
|
110 |
-
label="Output",
|
111 |
-
lines=20,
|
112 |
-
show_copy_button=True
|
113 |
-
)
|
114 |
-
|
115 |
-
gr.Markdown("---")
|
116 |
-
gr.Markdown("### Tips:")
|
117 |
-
gr.Markdown("- Supported formats: PDF, DOCX, TXT")
|
118 |
-
gr.Markdown("- Maximum file size: 200MB")
|
119 |
-
gr.Markdown("- Text can be pasted directly if you don't have a file")
|
120 |
-
gr.Markdown("- Uses HuggingFace Pro account with Cerebras access")
|
121 |
-
|
122 |
-
process_btn.click(
|
123 |
-
fn=process_document,
|
124 |
-
inputs=[document, operation_type, text_input],
|
125 |
-
outputs=output,
|
126 |
-
show_progress=True
|
127 |
-
)
|
128 |
-
|
129 |
-
if __name__ == "__main__":
|
130 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|