Soltane777 commited on
Commit
c3956b9
·
verified ·
1 Parent(s): b4d8928

Update backend/main.py

Browse files
Files changed (1) hide show
  1. backend/main.py +17 -4
backend/main.py CHANGED
@@ -45,7 +45,10 @@ async def extract_text(file: UploadFile = File(...)):
45
  @app.post("/summarize")
46
  async def summarize_text(text: str = Form(...)):
47
  summary = summarizer(text)
48
- return {"summary": summary[0]['summary_text']}
 
 
 
49
 
50
  # 3️⃣ تحليل الصور وتوليد وصف لها
51
  @app.post("/image_caption")
@@ -59,19 +62,29 @@ async def caption_image(file: UploadFile = File(...)):
59
 
60
  caption = image_captioning(image_bytes)
61
  os.remove(file_path)
62
- return {"caption": caption[0]['generated_text']}
 
 
 
 
63
 
64
  # 4️⃣ الإجابة على الأسئلة من النصوص
65
  @app.post("/qa/")
66
  async def answer_question(request: QARequest):
67
  answer = qa_pipeline(request.question, request.context)
68
- return {"answer": answer["answer"]}
 
 
 
69
 
70
  # 5️⃣ ترجمة المستندات
71
  @app.post("/translate")
72
  async def translate_text(text: str = Form(...)):
73
  translation = translator(text)
74
- return {"translation": translation[0]['translation_text']}
 
 
 
75
 
76
  # 6️⃣ توليد أكواد التصور من أوامر نصية
77
  @app.post("/generate_code")
 
45
  @app.post("/summarize")
46
  async def summarize_text(text: str = Form(...)):
47
  summary = summarizer(text)
48
+ if isinstance(summary, list) and len(summary) > 0 and "summary_text" in summary[0]:
49
+ return {"summary": summary[0]["summary_text"]}
50
+ else:
51
+ return {"error": "Summarization failed. No valid response."}
52
 
53
  # 3️⃣ تحليل الصور وتوليد وصف لها
54
  @app.post("/image_caption")
 
62
 
63
  caption = image_captioning(image_bytes)
64
  os.remove(file_path)
65
+
66
+ if isinstance(caption, list) and len(caption) > 0 and "generated_text" in caption[0]:
67
+ return {"caption": caption[0]["generated_text"]}
68
+ else:
69
+ return {"error": "Image captioning failed. No valid response received."}
70
 
71
  # 4️⃣ الإجابة على الأسئلة من النصوص
72
  @app.post("/qa/")
73
  async def answer_question(request: QARequest):
74
  answer = qa_pipeline(request.question, request.context)
75
+ if "answer" in answer:
76
+ return {"answer": answer["answer"]}
77
+ else:
78
+ return {"error": "No valid answer found."}
79
 
80
  # 5️⃣ ترجمة المستندات
81
  @app.post("/translate")
82
  async def translate_text(text: str = Form(...)):
83
  translation = translator(text)
84
+ if isinstance(translation, list) and len(translation) > 0 and "translation_text" in translation[0]:
85
+ return {"translation": translation[0]["translation_text"]}
86
+ else:
87
+ return {"error": "Translation failed. No valid response."}
88
 
89
  # 6️⃣ توليد أكواد التصور من أوامر نصية
90
  @app.post("/generate_code")