Spaces:
Sleeping
Sleeping
Commit ·
340e6c3
1
Parent(s): 4886118
Small changes+
Browse files
main.py
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI, File, UploadFile
|
| 2 |
from fastapi.responses import PlainTextResponse
|
| 3 |
-
from graphvision.extractor import GraphExtractor
|
| 4 |
-
from groq import Groq
|
| 5 |
-
import os
|
| 6 |
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
| 7 |
|
|
|
|
|
|
|
| 8 |
|
| 9 |
app = FastAPI(title="STEM Sight Backend")
|
| 10 |
|
| 11 |
-
|
| 12 |
app.add_middleware(
|
| 13 |
CORSMiddleware,
|
| 14 |
allow_origins=["*"], # Allows any browser extension to connect
|
|
@@ -17,7 +18,7 @@ app.add_middleware(
|
|
| 17 |
allow_headers=["*"],
|
| 18 |
)
|
| 19 |
|
| 20 |
-
# Initialize the Groq Client (
|
| 21 |
groq_client = Groq()
|
| 22 |
|
| 23 |
# Initialize your custom PyPI library
|
|
@@ -38,29 +39,46 @@ async def analyze_graph(file: UploadFile = File(...)):
|
|
| 38 |
|
| 39 |
# 2. Extract structured data using your library
|
| 40 |
print(f"Extracting data from {file.filename}...")
|
| 41 |
-
|
|
|
|
|
|
|
| 42 |
|
| 43 |
# Clean up the temporary file immediately
|
| 44 |
-
os.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
|
|
|
| 48 |
|
| 49 |
# 3. Format the JSON data into a prompt
|
| 50 |
-
graph_type = extraction_result
|
| 51 |
-
graph_data = extraction_result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
prompt = f"""
|
| 54 |
You are an accessibility assistant for visually impaired students.
|
| 55 |
I am giving you extracted data from a {graph_type} chart.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
Please summarize this data in one short, conversational, and easy-to-understand paragraph.
|
| 57 |
-
|
|
|
|
| 58 |
|
| 59 |
Data:
|
| 60 |
{graph_data}
|
| 61 |
"""
|
| 62 |
|
| 63 |
-
# 4. Send to Groq for lightning-fast inference
|
| 64 |
print("Generating audio script with Groq Llama 3...")
|
| 65 |
chat_completion = groq_client.chat.completions.create(
|
| 66 |
messages=[
|
|
@@ -70,7 +88,7 @@ async def analyze_graph(file: UploadFile = File(...)):
|
|
| 70 |
}
|
| 71 |
],
|
| 72 |
model="llama-3.1-8b-instant",
|
| 73 |
-
temperature=0.
|
| 74 |
)
|
| 75 |
|
| 76 |
# 5. Return strictly the text response for the Chrome extension to speak
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
from fastapi import FastAPI, File, UploadFile
|
| 4 |
from fastapi.responses import PlainTextResponse
|
|
|
|
|
|
|
|
|
|
| 5 |
from fastapi.middleware.cors import CORSMiddleware
|
| 6 |
+
from groq import Groq
|
| 7 |
|
| 8 |
+
# Import your newly updated PyPI library!
|
| 9 |
+
from graphvision import GraphExtractor
|
| 10 |
|
| 11 |
app = FastAPI(title="STEM Sight Backend")
|
| 12 |
|
|
|
|
| 13 |
app.add_middleware(
|
| 14 |
CORSMiddleware,
|
| 15 |
allow_origins=["*"], # Allows any browser extension to connect
|
|
|
|
| 18 |
allow_headers=["*"],
|
| 19 |
)
|
| 20 |
|
| 21 |
+
# Initialize the Groq Client (Looks for the GROQ_API_KEY environment variable)
|
| 22 |
groq_client = Groq()
|
| 23 |
|
| 24 |
# Initialize your custom PyPI library
|
|
|
|
| 39 |
|
| 40 |
# 2. Extract structured data using your library
|
| 41 |
print(f"Extracting data from {file.filename}...")
|
| 42 |
+
|
| 43 |
+
# 🚨 UPDATED: Call the new extract method
|
| 44 |
+
extraction_json_string = vision_engine.extract(temp_image_path)
|
| 45 |
|
| 46 |
# Clean up the temporary file immediately
|
| 47 |
+
if os.path.exists(temp_image_path):
|
| 48 |
+
os.remove(temp_image_path)
|
| 49 |
+
|
| 50 |
+
# 🚨 UPDATED: Parse the JSON string back into a Python dictionary
|
| 51 |
+
extraction_result = json.loads(extraction_json_string)
|
| 52 |
|
| 53 |
+
# 🚨 UPDATED: Check for the new error format from your library
|
| 54 |
+
if "error" in extraction_result:
|
| 55 |
+
return f"I'm sorry, I couldn't clearly identify the data in this graph. Reason: {extraction_result['error']}"
|
| 56 |
|
| 57 |
# 3. Format the JSON data into a prompt
|
| 58 |
+
graph_type = extraction_result.get("chart_type", "unknown")
|
| 59 |
+
graph_data = extraction_result.get("data", [])
|
| 60 |
+
|
| 61 |
+
# Grab optional labels/titles if they exist (good for context!)
|
| 62 |
+
x_label = extraction_result.get("x_axis_label", "Unknown X-Axis")
|
| 63 |
+
y_label = extraction_result.get("y_axis_label", "Unknown Y-Axis")
|
| 64 |
+
title = extraction_result.get("title", "Untitled Graph")
|
| 65 |
|
| 66 |
prompt = f"""
|
| 67 |
You are an accessibility assistant for visually impaired students.
|
| 68 |
I am giving you extracted data from a {graph_type} chart.
|
| 69 |
+
Title: {title}
|
| 70 |
+
X-Axis Label: {x_label}
|
| 71 |
+
Y-Axis Label: {y_label}
|
| 72 |
+
|
| 73 |
Please summarize this data in one short, conversational, and easy-to-understand paragraph.
|
| 74 |
+
Point out the largest and smallest values if relevant.
|
| 75 |
+
Do not use markdown, bold text, or asterisks. Write it exactly as it should be spoken out loud by a text-to-speech engine.
|
| 76 |
|
| 77 |
Data:
|
| 78 |
{graph_data}
|
| 79 |
"""
|
| 80 |
|
| 81 |
+
# 4. Send to Groq for lightning-fast inference
|
| 82 |
print("Generating audio script with Groq Llama 3...")
|
| 83 |
chat_completion = groq_client.chat.completions.create(
|
| 84 |
messages=[
|
|
|
|
| 88 |
}
|
| 89 |
],
|
| 90 |
model="llama-3.1-8b-instant",
|
| 91 |
+
temperature=0.4, # Lowered slightly for more factual summaries
|
| 92 |
)
|
| 93 |
|
| 94 |
# 5. Return strictly the text response for the Chrome extension to speak
|