Afeezee commited on
Commit
9fc8a1b
·
verified ·
1 Parent(s): d7cfcaf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -32
app.py CHANGED
@@ -47,7 +47,7 @@ def chunk_text(text, max_tokens=4000):
47
 
48
  def analyze_chunk(chunk):
49
  """
50
- Analyzes a single chunk of text using the Cerebras Llama model.
51
  """
52
  messages = [
53
  {
@@ -67,40 +67,18 @@ def analyze_chunk(chunk):
67
  ]
68
 
69
  try:
70
- # Use Cerebras AI for processing
71
  stream = client.chat.completions.create(
72
  messages=messages,
73
  model="llama-3.3-70b",
74
- stream=True,
75
  max_completion_tokens=1024,
76
  temperature=0.2,
77
  top_p=1
78
  )
79
- result = ""
80
- for chunk in stream:
81
- result += chunk.choices[0].delta.content or ""
82
- return result
83
  except Exception as e:
84
  return f"An error occurred while processing a chunk: {e}"
85
 
86
- from docx import Document
87
-
88
- def save_as_docx(content, file_name="Literature_Analysis.docx"):
89
- """
90
- Saves the given content to a DOCX file.
91
-
92
- Parameters:
93
- content (str): The text content to save.
94
- file_name (str): The name of the DOCX file (default: 'Literature_Analysis.docx').
95
- """
96
- document = Document()
97
- document.add_heading("Literature Analysis", level=1)
98
- document.add_paragraph(content)
99
- document.save(file_name)
100
- return file_name
101
-
102
- from docx import Document
103
-
104
  def save_as_docx(content, file_name="Literature_Analysis.docx"):
105
  """
106
  Saves the given content to a DOCX file.
@@ -158,11 +136,14 @@ def analyze_document(file):
158
  )
159
  final_summary = ""
160
  for chunk in stream:
161
- final_summary += chunk.choices[0].delta.content or ""
 
 
 
 
162
 
163
  # Save the final summary as a .docx file
164
  docx_file = save_as_docx(final_summary)
165
- yield f"**Final Summary:**\n\n{final_summary}"
166
  yield f"**Download the DOCX file here:** [Download {docx_file}](file:///{docx_file})"
167
  except Exception as e:
168
  yield f"**Error:** An error occurred during consolidation: {e}"
@@ -171,14 +152,11 @@ def analyze_document(file):
171
  interface = gr.Interface(
172
  fn=analyze_document,
173
  inputs=gr.File(label="Upload a PDF or DOCX file"),
174
- outputs=[
175
- gr.Markdown(label="Literature Analysis"),
176
- gr.File(label="Download Analysis (DOCX)")
177
- ],
178
  title="Automated Literature Review",
179
  description=(
180
  "Upload a PDF or DOCX document, and this tool will analyze it to extract and consolidate its content. "
181
- "You are advised to upload smaller documents with at most 8 pages."
182
  ),
183
  )
184
 
 
47
 
48
  def analyze_chunk(chunk):
49
  """
50
+ Analyzes a single chunk of text using the Cerebras model.
51
  """
52
  messages = [
53
  {
 
67
  ]
68
 
69
  try:
 
70
  stream = client.chat.completions.create(
71
  messages=messages,
72
  model="llama-3.3-70b",
73
+ stream=False,
74
  max_completion_tokens=1024,
75
  temperature=0.2,
76
  top_p=1
77
  )
78
+ return stream.choices[0].message.content
 
 
 
79
  except Exception as e:
80
  return f"An error occurred while processing a chunk: {e}"
81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
  def save_as_docx(content, file_name="Literature_Analysis.docx"):
83
  """
84
  Saves the given content to a DOCX file.
 
136
  )
137
  final_summary = ""
138
  for chunk in stream:
139
+ content = chunk.choices[0].delta.content or ""
140
+ final_summary += content
141
+
142
+ # Show the final summary first
143
+ yield f"**Final Summary:**\n\n{final_summary}"
144
 
145
  # Save the final summary as a .docx file
146
  docx_file = save_as_docx(final_summary)
 
147
  yield f"**Download the DOCX file here:** [Download {docx_file}](file:///{docx_file})"
148
  except Exception as e:
149
  yield f"**Error:** An error occurred during consolidation: {e}"
 
152
  interface = gr.Interface(
153
  fn=analyze_document,
154
  inputs=gr.File(label="Upload a PDF or DOCX file"),
155
+ outputs=gr.Markdown(label="Literature Analysis"),
 
 
 
156
  title="Automated Literature Review",
157
  description=(
158
  "Upload a PDF or DOCX document, and this tool will analyze it to extract and consolidate its content. "
159
+ "It might take a while, be patient. You are advised to upload smaller documents with shorter text as it may take a while to process longer files."
160
  ),
161
  )
162