|
import gradio as gr |
|
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM |
|
import pytesseract |
|
|
|
|
|
from transformers import pipeline |
|
|
|
|
|
tokenizer = AutoTokenizer.from_pretrained("sambanovasystems/SambaLingo-Arabic-Chat") |
|
model = AutoModelForCausalLM.from_pretrained("sambanovasystems/SambaLingo-Arabic-Chat") |
|
|
|
|
|
def chat_fn(history, user_input): |
|
conversation = {"history": history, "user": user_input} |
|
|
|
input_ids = tokenizer.encode(user_input, return_tensors="pt") |
|
response = model.generate(input_ids=input_ids, max_length=50) |
|
conversation["bot"] = tokenizer.decode(response[0], skip_special_tokens=True) |
|
history.append((user_input, conversation["bot"])) |
|
return history, "" |
|
|
|
|
|
def ocr(image): |
|
text = pytesseract.image_to_string(image) |
|
return text |
|
|
|
|
|
with gr.Blocks() as demo: |
|
gr.Markdown("### الصور والدردشة") |
|
|
|
|
|
with gr.Tab("استخراج النصوص من الصور"): |
|
with gr.Row(): |
|
image_input = gr.Image(type="pil") |
|
ocr_output = gr.Textbox() |
|
submit_button = gr.Button("Submit") |
|
submit_button.click(ocr, inputs=image_input, outputs=ocr_output) |
|
|
|
|
|
with gr.Tab("المحادثة"): |
|
chatbot = gr.Chatbot() |
|
msg = gr.Textbox(label="اكتب رسالتك") |
|
clear = gr.Button("Clear") |
|
msg.submit(chat_fn, [chatbot, msg], [chatbot, msg]) |
|
clear.click(lambda: None, None, chatbot) |
|
|
|
|
|
demo.launch() |