Gastric and esophageal cancers, the predominant forms of upper gastrointestinal malignancies, contribute significantly to global cancer mortality. Routine detection methods, including medical imaging, endoscopic examination, and pathological biopsy, often suffer from drawbacks such as low sensitivity and laborious and complex procedures. Raman spectroscopy is a non-invasive and label-free optical technique that provides highly sensitive biomolecular information to facilitate effective tumor identification. In this work, we report the use of fiber-optic Raman spectroscopy for the accurate and rapid diagnosis of gastric and esophageal cancers. Using a database of 14,000 spectra from 140 ex vivo tissue pieces of both tumor and normal tissue samples, we compare the random forest (RF) and our established Euclidean distance Raman spectroscopy (EDRS) model. The RF analysis achieves a sensitivity of 85.23% and an accuracy of 83.05% in diagnosing gastric tumors. The EDRS algorithm with improved diagnostic transparency further increases the sensitivity to 92.86% and accuracy to 89.29%. When these diagnostic protocols are extended to esophageal tumors, the RF and EDRS models achieve accuracies of 71.27% and 93.18%, respectively. Finally, we demonstrate that fewer than 20 spectra are sufficient to achieve good Raman diagnostic accuracy for both tumor tissues. This optimizes the balance between acquisition time and diagnostic performance. Our work, although conducted on ex vivo tissue models, offers valuable insights for in vivo in situ endoscopic Raman diagnosis of gastric and esophageal cancer lesions in the future. Our study provides a robust, rapid, and convenient method as a new paradigm in in vivo endoscopic medical diagnostics that integrates spectroscopic techniques and aRaman probe for detecting upper gastrointestinal malignancies.