Ayush0804 commited on
Commit
4ab5238
·
verified ·
1 Parent(s): cefb24f

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -138
app.py DELETED
@@ -1,138 +0,0 @@
1
- from langchain.prompts import PromptTemplate
2
- from langchain_huggingface import HuggingFaceEndpoint
3
- from PIL import Image
4
- import os
5
- import secrets
6
- from pathlib import Path
7
- import tempfile
8
- import gradio as gr
9
-
10
- # Initialize the Hugging Face BLIP model
11
- image_captioning_model = HuggingFaceEndpoint(
12
- endpoint_url="https://api-inference.huggingface.co/models/Salesforce/blip-image-captioning-base",
13
- huggingfacehub_api_token=os.getenv("HUGGING_FACE_API"), # Ensure you set this in your environment
14
- temperature=0.7,
15
- max_new_tokens=1024,
16
- )
17
- math_llm=HuggingFaceEndpoint(
18
- endpoint_url="https://api-inference.huggingface.co/models/Qwen/Qwen2.5-Math-7B-Instruct",
19
- huggingfacehub_api_token=os.getenv("HUGGING_FACE_API"), # Ensure you set this in your environment
20
- temperature=0.7,
21
- max_new_tokens=1024,)
22
- # Function to process the image
23
- def process_image(image, shouldConvert=False):
24
- # Ensure temporary directory exists
25
- uploaded_file_dir = os.environ.get("GRADIO_TEMP_DIR") or str(
26
- Path(tempfile.gettempdir()) / "gradio"
27
- )
28
- os.makedirs(uploaded_file_dir, exist_ok=True)
29
-
30
- # Save the uploaded image
31
- name = f"tmp{secrets.token_hex(20)}.jpg"
32
- filename = os.path.join(uploaded_file_dir, name)
33
- if shouldConvert:
34
- # Convert image to RGB mode if it contains transparency
35
- new_img = Image.new("RGB", size=(image.width, image.height), color=(255, 255, 255))
36
- new_img.paste(image, (0, 0), mask=image)
37
- image = new_img
38
- image.save(filename)
39
-
40
- # Define a PromptTemplate for text instruction
41
- template = """
42
- You are a helpful AI assistant.
43
- Please describe the math-related content in this image, ensuring that any LaTeX formulas are correctly transcribed.
44
- Non-mathematical details do not need to be described.
45
-
46
- Image Path: {image}
47
- """
48
- prompt_template = PromptTemplate(
49
- input_variables=["image"], # Dynamically insert the image path
50
- template=template
51
- )
52
-
53
- # Create the text instruction by rendering the prompt template
54
- prompt = prompt_template.format(image=f"file://{filename}")
55
-
56
- # Use the model with both the image and the generated prompt
57
- with open(filename, "rb") as img_file:
58
- response = image_captioning_model({
59
- "inputs": {
60
- "image": img_file,
61
- "text": prompt
62
- }
63
- })
64
-
65
- # Return the model's response
66
- return response
67
-
68
- def get_math_response(image_description, user_question):
69
- template = """
70
- You are a helpful AI assistant specialized in solving math reasoning problems.
71
- Analyze the following question carefully and provide a step-by-step explanation along with the answer.
72
- Image description : {image_description}
73
- Question: {user_question}?
74
- """
75
-
76
- prompt_template = PromptTemplate(
77
- input_variables=["user_question","image_description"], # Define the placeholder(s) in the template
78
- template=template
79
- )
80
- formatted_prompt = prompt_template.format(user_question=user_question, image_description=image_description)
81
-
82
- # Pass the formatted prompt to the model
83
- response = math_llm(formatted_prompt)
84
-
85
- # Print the response
86
- yield response
87
-
88
- def math_chat_bot(image, sketchpad, question, state):
89
- current_tab_index = state["tab_index"]
90
- image_description = None
91
- # Upload
92
- if current_tab_index == 0:
93
- if image is not None:
94
- image_description = process_image(image)
95
- # Sketch
96
- elif current_tab_index == 1:
97
- print(sketchpad)
98
- if sketchpad and sketchpad["composite"]:
99
- image_description = process_image(sketchpad["composite"], True)
100
- yield from get_math_response(image_description, question)
101
- css = """
102
- #qwen-md .katex-display { display: inline; }
103
- #qwen-md .katex-display>.katex { display: inline; }
104
- #qwen-md .katex-display>.katex>.katex-html { display: inline; }
105
- """
106
-
107
- def tabs_select(e: gr.SelectData, _state):
108
- _state["tab_index"] = e.index
109
- return _state
110
-
111
- with gr.Blocks(css=css) as demo:
112
- state = gr.State({"tab_index": 0})
113
-
114
- with gr.Row():
115
- with gr.Column():
116
- with gr.Tabs() as input_tabs:
117
- with gr.Tab("Upload"):
118
- input_image = gr.Image(type="pil", label="Upload")
119
- with gr.Tab("Sketch"):
120
- input_sketchpad = gr.Sketchpad(type="pil", label="Sketch", layers=False)
121
-
122
- input_tabs.select(fn=tabs_select, inputs=[state], outputs=[state])
123
-
124
- input_text = gr.Textbox(label="Input your question")
125
- with gr.Row():
126
- clear_btn = gr.ClearButton([input_image, input_sketchpad, input_text])
127
- submit_btn = gr.Button("Submit", variant="primary")
128
-
129
- with gr.Column():
130
- output_md = gr.Markdown(label="Answer", elem_id="qwen-md")
131
-
132
- submit_btn.click(
133
- fn=math_chat_bot,
134
- inputs=[input_image, input_sketchpad, input_text, state],
135
- outputs=output_md,
136
- )
137
-
138
- demo.launch()