samvb1002 commited on
Commit
ad578b5
·
verified ·
1 Parent(s): 0878aee

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -21
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("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()
 
 
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()