Update app.py
Browse files
app.py
CHANGED
|
@@ -8,33 +8,42 @@ from report_generator import (
|
|
| 8 |
generate_pdf_report,
|
| 9 |
generate_diagram_report,
|
| 10 |
)
|
| 11 |
-
|
|
|
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
print("AI model failed to load, falling back to rule-based parsing:", e)
|
| 18 |
-
ai_parser = None
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
# Global containers for pending tasks and generated files.
|
| 40 |
task_bucket = [] # Queue of tasks waiting to be processed.
|
|
@@ -48,8 +57,8 @@ def process_single_task(task):
|
|
| 48 |
for remaining in range(5, 0, -1):
|
| 49 |
task["timer"] = remaining
|
| 50 |
time.sleep(1)
|
| 51 |
-
# Use
|
| 52 |
-
intent =
|
| 53 |
cmd = task["command"].lower()
|
| 54 |
|
| 55 |
# Process based on the detected intent.
|
|
|
|
| 8 |
generate_pdf_report,
|
| 9 |
generate_diagram_report,
|
| 10 |
)
|
| 11 |
+
import cohere
|
| 12 |
+
from cohere.classify import ClassificationExample
|
| 13 |
|
| 14 |
+
# Initialize the Cohere client using your provided API key.
|
| 15 |
+
# (For security, consider loading the API key from environment variables.)
|
| 16 |
+
COHERE_API_KEY = "ffwtfyfLwqSOtw65psZardfqhUxMr1h7u5vzYotI"
|
| 17 |
+
co = cohere.Client(COHERE_API_KEY)
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
# Define some labeled examples for classification.
|
| 20 |
+
cohere_examples = [
|
| 21 |
+
ClassificationExample(text="generate a report on unemployment", label="report"),
|
| 22 |
+
ClassificationExample(text="create a report on market trends", label="report"),
|
| 23 |
+
ClassificationExample(text="generate diagram of sales data", label="diagram"),
|
| 24 |
+
ClassificationExample(text="create a diagram of quarterly revenue", label="diagram")
|
| 25 |
+
]
|
| 26 |
+
|
| 27 |
+
def cohere_parse_command(command):
|
| 28 |
+
"""Uses Cohere's classify endpoint to determine the intent of the command."""
|
| 29 |
+
try:
|
| 30 |
+
response = co.classify(
|
| 31 |
+
model="large",
|
| 32 |
+
inputs=[command],
|
| 33 |
+
examples=cohere_examples
|
| 34 |
+
)
|
| 35 |
+
# Get the prediction for the first input.
|
| 36 |
+
prediction = response.classifications[0].prediction
|
| 37 |
+
return prediction
|
| 38 |
+
except Exception as e:
|
| 39 |
+
print("Cohere classification error, falling back to keyword search:", e)
|
| 40 |
+
lower = command.lower()
|
| 41 |
+
if "diagram" in lower:
|
| 42 |
+
return "diagram"
|
| 43 |
+
elif "report" in lower:
|
| 44 |
+
return "report"
|
| 45 |
+
else:
|
| 46 |
+
return "unknown"
|
| 47 |
|
| 48 |
# Global containers for pending tasks and generated files.
|
| 49 |
task_bucket = [] # Queue of tasks waiting to be processed.
|
|
|
|
| 57 |
for remaining in range(5, 0, -1):
|
| 58 |
task["timer"] = remaining
|
| 59 |
time.sleep(1)
|
| 60 |
+
# Use Cohere (or fallback) to classify the intent.
|
| 61 |
+
intent = cohere_parse_command(task["command"])
|
| 62 |
cmd = task["command"].lower()
|
| 63 |
|
| 64 |
# Process based on the detected intent.
|