LahiruD95 commited on
Commit
859566c
·
1 Parent(s): f5871c5

Chnaged requirement text

Browse files
Files changed (1) hide show
  1. app/routes.py +64 -32
app/routes.py CHANGED
@@ -65,42 +65,74 @@ def transcribe():
65
  return error_response(str(e), 500)
66
 
67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
  @bp.route('/analyze_image', methods=['POST'])
69
  def analyze_image():
70
- if 'file' not in request.files:
71
- return error_response("No image file provided", 400)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
 
73
- file = request.files['file']
74
- image_bytes = file.read()
75
 
76
- try:
77
- # send raw bytes to HF inference
78
- result = ocr_api(image_bytes)
79
- # TroCR returns a single string of text
80
- extracted = ""
81
- if isinstance(result, str):
82
- extracted = result
83
- elif isinstance(result, dict) and "generated_text" in result:
84
- extracted = result["generated_text"]
85
- else:
86
- # fallback to printing whatever we got
87
- extracted = str(result)
88
-
89
- extracted = extracted.strip()
90
- if not extracted:
91
- return error_response("No text extracted from image", 400)
92
-
93
- analysis = analyze_text_internal(extracted)
94
- tags = generate_tags(extracted)
95
- return jsonify({
96
- "extracted_text": extracted,
97
- "sentiment": analysis["sentiment"],
98
- "emotion": analysis["emotion"],
99
- "confidence": analysis["confidence"],
100
- "tags": tags
101
- })
102
- except Exception as e:
103
- return error_response(str(e), 500)
104
 
105
 
106
  # Internal function to call analyze_text directly
 
65
  return error_response(str(e), 500)
66
 
67
 
68
+ # @bp.route('/analyze_image', methods=['POST'])
69
+ # def analyze_image():
70
+ # if 'file' not in request.files:
71
+ # return error_response("No image file provided", 400)
72
+ #
73
+ # file = request.files['file']
74
+ # image_bytes = file.read()
75
+ #
76
+ # try:
77
+ # # send raw bytes to HF inference
78
+ # result = ocr_api(image_bytes)
79
+ # # TroCR returns a single string of text
80
+ # extracted = ""
81
+ # if isinstance(result, str):
82
+ # extracted = result
83
+ # elif isinstance(result, dict) and "generated_text" in result:
84
+ # extracted = result["generated_text"]
85
+ # else:
86
+ # # fallback to printing whatever we got
87
+ # extracted = str(result)
88
+ #
89
+ # extracted = extracted.strip()
90
+ # if not extracted:
91
+ # return error_response("No text extracted from image", 400)
92
  @bp.route('/analyze_image', methods=['POST'])
93
  def analyze_image():
94
+ if 'file' not in request.files:
95
+ return error_response("No image file provided", 400)
96
+
97
+ f = request.files['file']
98
+ path = os.path.join("/tmp", secure_filename(f.filename))
99
+ f.save(path)
100
+
101
+ # read raw bytes
102
+ with open(path, "rb") as img_f:
103
+ img_bytes = img_f.read()
104
+
105
+ try:
106
+ # 1) Ask the vision-LLM to describe / extract text
107
+ completion = hf.chat.completions.create(
108
+ model="google/gemma-3-27b-it",
109
+ messages=[{
110
+ "role": "user",
111
+ "content": [
112
+ {"type": "text", "text": "Extract any text you see in this image."},
113
+ {"type": "image_bytes", "image_bytes": {"data": img_bytes}}
114
+ ]
115
+ }],
116
+ max_tokens=512,
117
+ )
118
+
119
+ extracted = completion.choices[0].message.content.strip();
120
+
121
+ analysis = analyze_text_internal(extracted)
122
+ tags = generate_tags(extracted)
123
+ return jsonify({
124
+ "extracted_text": extracted,
125
+ "sentiment": analysis["sentiment"],
126
+ "emotion": analysis["emotion"],
127
+ "confidence": analysis["confidence"],
128
+ "tags": tags
129
+ })
130
+ except Exception as e:
131
+ return error_response(str(e), 500)
132
+
133
+
134
 
 
 
135
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
136
 
137
 
138
  # Internal function to call analyze_text directly