Skip to content

Commit

Permalink
Merge branch 'latex'
Browse files Browse the repository at this point in the history
  • Loading branch information
gracetxgao committed Sep 15, 2024
2 parents 4448432 + 99406fd commit 54259a8
Show file tree
Hide file tree
Showing 12 changed files with 343 additions and 73 deletions.
75 changes: 42 additions & 33 deletions backend/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
from flask_cors import CORS
from num2words import num2words
import re
from io import BytesIO
import base64
import requests

def convert_to_words(input_string):
# Define a dictionary to map symbols to words
Expand Down Expand Up @@ -41,6 +41,8 @@ def replace_number(match):

CORS(app)

CORS(app)

# Load environment variables from the .env file
load_dotenv()

Expand Down Expand Up @@ -71,14 +73,13 @@ def index():
def gpt_query():
# Get the query from the URL parameters
query = convert_to_words(request.args.get('text'))
user_id = "LeoZ"
instance_id = "001"
user_id = request.args.get('user_id')
instance_id = request.args.get('instance_id')
# Access the OPENAI_KEY environment variable
openai_key = os.getenv("OPENAI_KEY")

if not query or not user_id or not instance_id:
return jsonify({'error': 'Query parameter is missing'}), 400

try:
client = openai.OpenAI(api_key=openai_key)
completion = client.chat.completions.create(
Expand All @@ -88,30 +89,38 @@ def gpt_query():
{"role": "user", "content": query}
]
)

response = completion.choices[0].message.content
# Return the message
logger.info(response)
# Convert LaTeX to PNG (tex_to_png should return binary data)
img_binary = tex_to_png(response, user_id, instance_id)

# Convert binary image data to Base64 string
img_base64 = base64.b64encode(img_binary).decode('utf-8')

logger.info(response)
logger.info(img_base64)

# Return both LaTeX code and the image in Base64 format
return jsonify({
'latex_code': response,
'img_binary': img_base64
})

latex_code = completion.choices[0].message.content
logger.info(latex_code)
# Make a GET request to the /tex_png endpoint with LaTeX and user info
response = requests.get(
'http://localhost:3001/tex_to_png', # Change this if hosted differently
json={
'latex_string': latex_code,
'user_id': user_id,
'instance_id': instance_id
}
)
# Handle the response from /tex_png
if response.status_code == 200:
img_base64 = response.json().get('img_base64')
logger.info(img_base64)
return jsonify({
'latex_code': latex_code,
'img_base64': img_base64
})
else:
return jsonify({'error': 'Failed to generate PNG'}), 500
except Exception as e:
return jsonify({'error': str(e)}), 500

@app.route('/tex_png', methods=['POST'])
def tex_to_png(latex_string, user_id, instance_id):
@app.route('/tex_to_png', methods=['GET'])
def tex_to_png():
latex_string = request.json.get('latex_string')
user_id = request.json.get('user_id')
instance_id = request.json.get('instance_id')
logger.info(latex_string)
logger.info(user_id)
logger.info(instance_id)
if not latex_string:
return jsonify({"error": "No LaTeX string provided"}), 400
png_name = f'{user_id}_{instance_id}.png'
Expand Down Expand Up @@ -147,7 +156,7 @@ def tex_to_png(latex_string, user_id, instance_id):

# Upload the PNG to S3 with the user_id and instance_id as part of the object key
try:
with open('output/LeoZ_001.png', 'rb') as png_file:
with open(f'output/{png_name}', 'rb') as png_file:
s3_client.upload_fileobj(
png_file,
S3_BUCKET_NAME,
Expand All @@ -159,7 +168,7 @@ def tex_to_png(latex_string, user_id, instance_id):
return jsonify({"error": f"Failed to upload to S3: {str(e)}"}), 500

try:
with open('output/LeoZ_001.png', 'rb') as png_file:
with open(f'output/{png_name}', 'rb') as png_file:
png_binary = png_file.read()
logger.info(f"Saved image binary.")
except Exception as e:
Expand All @@ -176,16 +185,16 @@ def tex_to_png(latex_string, user_id, instance_id):
except FileNotFoundError as e:
print(f"File not found: {e}")

# Convert binary image data to Base64 string
img_base64 = base64.b64encode(png_binary).decode('utf-8')

logger.info("Tex_to_PNG full success!")
return png_binary
return jsonify({'img_base64': img_base64}), 200

@app.route('/delete', methods=['POST'])
def delete():
"""
Endpoint to delete PNG from S3 based on user_id and instance_id
"""
user_id = "LeoZ"
instance_id = "001"
user_id = request.args.get('user_id')
instance_id = request.args.get('instance_id')

if not user_id or not instance_id:
return jsonify({"error": "No user ID or instance ID provided"}), 400
Expand Down
3 changes: 2 additions & 1 deletion backend/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ openai
python-dotenv
boto3
flask-cors
num2words
num2words
requests
100 changes: 100 additions & 0 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"@emotion/react": "^11.13.0",
"@emotion/styled": "^11.13.0",
"@types/dom-speech-recognition": "^0.0.4",
"axios": "^1.7.7",
"dotenv": "^16.4.5",
"firebase": "^10.13.1",
"framer-motion": "^11.3.28",
Expand Down
36 changes: 36 additions & 0 deletions frontend/src/equationService.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import axios from 'axios';

export const fetchEquationData = async ( text: string, user_id: string, instance_id: string) => {
try {
const response = await axios.get('http://localhost:3001/gpt-query', {
params: {
text,
user_id,
instance_id
}
});
console.log('response from latex api', response.data);

return response.data;
} catch (error) {
console.error('Error fetching equation data:', error);
throw error;
}
};

export const fetchEdited = async (userId: string, equationId: string, text: string) => {
try {
const response = await axios.get('http://localhost:3001/gpt-query', {
params: {
userId,
equationId,
text
}
});
return response.data;
} catch (error) {
console.error('Error fetching edited response:', error);
throw error;
}
};

Loading

0 comments on commit 54259a8

Please sign in to comment.