Upload app.py
Browse files
app.py
CHANGED
@@ -1,24 +1,45 @@
|
|
1 |
|
2 |
import gradio as gr
|
3 |
-
import
|
|
|
|
|
4 |
|
5 |
-
#
|
6 |
-
|
7 |
-
reader = easyocr.Reader(['ar', 'en']) # يدعم العربية والإنجليزية
|
8 |
-
result = reader.readtext(image_path, detail=0)
|
9 |
-
return " ".join(result)
|
10 |
|
11 |
-
#
|
12 |
-
def
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
return text
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
-
|
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()
|
|