|
|
|
import gradio as gr |
|
from transformers import pipeline |
|
from PIL import Image |
|
import pytesseract |
|
|
|
|
|
chat_model = pipeline("conversational", model="microsoft/DialoGPT-medium") |
|
|
|
|
|
def chat_fn(history, user_input): |
|
conversation = {"user": user_input, "bot": None} |
|
if history: |
|
conversation["history"] = history |
|
response = chat_model(conversation["user"]) |
|
conversation["bot"] = response[0]["generated_text"] |
|
history.append((user_input, conversation["bot"])) |
|
return history, "" |
|
|
|
|
|
def ocr(image): |
|
text = pytesseract.image_to_string(Image.open(image)) |
|
return text |
|
|
|
|
|
with gr.Blocks() as demo: |
|
gr.Markdown("### استخلاص النصوص من الصور والدردشة") |
|
|
|
|
|
with gr.Tab("استخلاص النصوص من الصور"): |
|
with gr.Row(): |
|
image_input = gr.Image(label="اختر صورة") |
|
ocr_output = gr.Textbox(label="النص المستخلص") |
|
submit_button = gr.Button("Submit") |
|
submit_button.click(ocr, inputs=[image_input], outputs=[ocr_output]) |
|
|
|
|
|
with gr.Tab("محادثة"): |
|
chatbot = gr.Chatbot() |
|
message = gr.Textbox(label="رسالتك هنا") |
|
state = gr.State([]) |
|
send_button = gr.Button("إرسال") |
|
send_button.click(chat_fn, inputs=[state, message], outputs=[chatbot, state]) |
|
|
|
demo.launch() |
|
|