Update app.py
Browse files
app.py
CHANGED
|
@@ -1,45 +1,42 @@
|
|
| 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("
|
| 9 |
|
| 10 |
# Chat function
|
| 11 |
def chat_fn(history, user_input):
|
| 12 |
-
conversation = {"
|
| 13 |
-
|
| 14 |
-
|
| 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(
|
| 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(
|
| 33 |
-
ocr_output = gr.Textbox(
|
| 34 |
submit_button = gr.Button("Submit")
|
| 35 |
-
submit_button.click(ocr, inputs=
|
| 36 |
-
|
| 37 |
# Chat section
|
| 38 |
-
with gr.Tab("
|
| 39 |
chatbot = gr.Chatbot()
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
|
| 45 |
-
demo.launch()
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
from PIL import Image
|
| 4 |
import pytesseract
|
| 5 |
|
| 6 |
# Initialize chat model
|
| 7 |
+
chat_model = pipeline("text-generation", model="gpt2") # عدّل اسم النموذج حسب الحاجة
|
| 8 |
|
| 9 |
# Chat function
|
| 10 |
def chat_fn(history, user_input):
|
| 11 |
+
conversation = {"history": history, "user": user_input}
|
| 12 |
+
response = chat_model(user_input, max_length=50, num_return_sequences=1)
|
| 13 |
+
conversation["bot"] = response[0]['generated_text']
|
|
|
|
|
|
|
| 14 |
history.append((user_input, conversation["bot"]))
|
| 15 |
return history, ""
|
| 16 |
|
| 17 |
# OCR function
|
| 18 |
def ocr(image):
|
| 19 |
+
text = pytesseract.image_to_string(image)
|
| 20 |
return text
|
| 21 |
|
| 22 |
# Gradio interface
|
| 23 |
with gr.Blocks() as demo:
|
| 24 |
+
gr.Markdown("### الصور والدردشة")
|
| 25 |
+
|
| 26 |
# Image OCR section
|
| 27 |
+
with gr.Tab("استخراج النصوص من الصور"):
|
| 28 |
with gr.Row():
|
| 29 |
+
image_input = gr.Image(type="pil")
|
| 30 |
+
ocr_output = gr.Textbox()
|
| 31 |
submit_button = gr.Button("Submit")
|
| 32 |
+
submit_button.click(ocr, inputs=image_input, outputs=ocr_output)
|
| 33 |
+
|
| 34 |
# Chat section
|
| 35 |
+
with gr.Tab("المحادثة"):
|
| 36 |
chatbot = gr.Chatbot()
|
| 37 |
+
msg = gr.Textbox(label="اكتب رسالتك")
|
| 38 |
+
clear = gr.Button("Clear")
|
| 39 |
+
msg.submit(chat_fn, [chatbot, msg], [chatbot, msg])
|
| 40 |
+
clear.click(lambda: None, None, chatbot)
|
| 41 |
|
| 42 |
+
demo.launch()
|