samvb1002 commited on
Commit
88bccf2
·
verified ·
1 Parent(s): 6ec8ad7

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -17
app.py CHANGED
@@ -1,24 +1,45 @@
1
 
2
  import gradio as gr
3
- import easyocr
 
 
4
 
5
- # تعريف وظيفة لتحليل النصوص من الصور
6
- def extract_text_from_image(image_path):
7
- reader = easyocr.Reader(['ar', 'en']) # يدعم العربية والإنجليزية
8
- result = reader.readtext(image_path, detail=0)
9
- return " ".join(result)
10
 
11
- # إنشاء واجهة باستخدام Gradio
12
- def process_image(image):
13
- text = extract_text_from_image(image)
 
 
 
 
 
 
 
 
 
 
14
  return text
15
 
16
- interface = gr.Interface(
17
- fn=process_image,
18
- inputs=gr.Image(type="filepath"),
19
- outputs="text",
20
- title="استخراج النصوص من الصور"
21
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
- if __name__ == "__main__":
24
- interface.launch()
 
1
 
2
  import gradio as gr
3
+ from transformers import pipeline
4
+ from PIL import Image
5
+ import pytesseract
6
 
7
+ # Initialize chat model
8
+ chat_model = pipeline("conversational", model="microsoft/DialoGPT-medium")
 
 
 
9
 
10
+ # Chat function
11
+ def chat_fn(history, user_input):
12
+ conversation = {"user": user_input, "bot": None}
13
+ if history:
14
+ conversation["history"] = history
15
+ response = chat_model(conversation["user"])
16
+ conversation["bot"] = response[0]["generated_text"]
17
+ history.append((user_input, conversation["bot"]))
18
+ return history, ""
19
+
20
+ # OCR function
21
+ def ocr(image):
22
+ text = pytesseract.image_to_string(Image.open(image))
23
  return text
24
 
25
+ # Gradio interface
26
+ with gr.Blocks() as demo:
27
+ gr.Markdown("### استخلاص النصوص من الصور والدردشة")
28
+
29
+ # Image OCR section
30
+ with gr.Tab("استخلاص النصوص من الصور"):
31
+ with gr.Row():
32
+ image_input = gr.Image(label="اختر صورة")
33
+ ocr_output = gr.Textbox(label="النص المستخلص")
34
+ submit_button = gr.Button("Submit")
35
+ submit_button.click(ocr, inputs=[image_input], outputs=[ocr_output])
36
+
37
+ # Chat section
38
+ with gr.Tab("محادثة"):
39
+ chatbot = gr.Chatbot()
40
+ message = gr.Textbox(label="رسالتك هنا")
41
+ state = gr.State([])
42
+ send_button = gr.Button("إرسال")
43
+ send_button.click(chat_fn, inputs=[state, message], outputs=[chatbot, state])
44
 
45
+ demo.launch()