Fred808 commited on
Commit
7a2125e
·
verified ·
1 Parent(s): 39af76e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -4
app.py CHANGED
@@ -845,11 +845,54 @@ async def get_analytics():
845
  "average_sentiment": avg_sentiment
846
  }
847
 
848
- @app.post("/voice")
 
 
 
 
 
 
 
 
 
 
 
 
849
  async def process_voice(file: UploadFile = File(...)):
850
- contents = await file.read()
851
- simulated_text = "Simulated speech-to-text conversion result."
852
- return {"transcription": simulated_text}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
853
 
854
  # --- Payment Callback Endpoint with Payment Tracking and Redirection ---
855
  @app.api_route("/payment_callback", methods=["GET", "POST"])
 
845
  "average_sentiment": avg_sentiment
846
  }
847
 
848
+ # Load the Hugging Face API token from environment variables
849
+ HUGGING_FACE_API_TOKEN = os.getenv("HUGGING_FACE_API_TOKEN")
850
+ if not HUGGING_FACE_API_TOKEN:
851
+ raise ValueError("Hugging Face API token not found in environment variables.")
852
+
853
+ # Hugging Face Whisper API configuration
854
+ WHISPER_API_URL = "https://router.huggingface.co/fal-ai"
855
+ WHISPER_API_HEADERS = {"Authorization": f"Bearer {HUGGING_FACE_API_TOKEN}"}
856
+
857
+ class TranscriptionResponse(BaseModel):
858
+ transcription: str
859
+
860
+ @app.post("/voice", response_model=TranscriptionResponse)
861
  async def process_voice(file: UploadFile = File(...)):
862
+ """
863
+ Endpoint to process voice notes and transcribe them using OpenAI Whisper via Hugging Face.
864
+ """
865
+ try:
866
+ # Read the uploaded file
867
+ contents = await file.read()
868
+
869
+ # Save the file temporarily (optional, depending on how the API expects the input)
870
+ temp_file_path = f"temp_{file.filename}"
871
+ with open(temp_file_path, "wb") as temp_file:
872
+ temp_file.write(contents)
873
+
874
+ # Call the Hugging Face Whisper API
875
+ with open(temp_file_path, "rb") as audio_file:
876
+ response = requests.post(
877
+ WHISPER_API_URL,
878
+ headers=WHISPER_API_HEADERS,
879
+ files={"file": audio_file}
880
+ )
881
+
882
+ # Clean up the temporary file
883
+ os.remove(temp_file_path)
884
+
885
+ # Check if the API call was successful
886
+ if response.status_code != 200:
887
+ raise HTTPException(status_code=response.status_code, detail="Failed to transcribe audio.")
888
+
889
+ # Extract the transcription from the API response
890
+ transcription = response.json().get("text", "")
891
+
892
+ return {"transcription": transcription}
893
+
894
+ except Exception as e:
895
+ raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
896
 
897
  # --- Payment Callback Endpoint with Payment Tracking and Redirection ---
898
  @app.api_route("/payment_callback", methods=["GET", "POST"])