wahab5763 commited on
Commit
535cfc5
·
verified ·
1 Parent(s): d565660

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -0
app.py CHANGED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import pipeline
3
+ import requests
4
+
5
+ # Load OCR model for extracting text from images
6
+ @st.cache_resource
7
+ def load_ocr_model():
8
+ return pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
9
+
10
+ # Function to interact with ChatGPT API
11
+ def chat_with_gpt(prompt):
12
+ api_key = st.secrets["OPENAI_API_KEY"]
13
+ url = "https://api.openai.com/v1/completions"
14
+ headers = {
15
+ "Authorization": f"Bearer {api_key}",
16
+ "Content-Type": "application/json",
17
+ }
18
+ data = {
19
+ "model": "text-davinci-003",
20
+ "prompt": prompt,
21
+ "max_tokens": 1000,
22
+ "temperature": 0.7,
23
+ }
24
+ response = requests.post(url, headers=headers, json=data)
25
+ if response.status_code == 200:
26
+ return response.json().get("choices")[0].get("text", "").strip()
27
+ else:
28
+ st.error(f"Error from OpenAI: {response.status_code}")
29
+ return None
30
+
31
+ # Streamlit App
32
+ def main():
33
+ st.title("Image-to-Text with ChatGPT")
34
+ st.markdown(
35
+ """
36
+ **Upload an image**, extract text using a state-of-the-art OCR model,
37
+ and get explanations or solutions using ChatGPT.
38
+ """
39
+ )
40
+
41
+ uploaded_file = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg"])
42
+
43
+ if uploaded_file is not None:
44
+ st.image(uploaded_file, caption="Uploaded Image", use_column_width=True)
45
+ st.write("Processing...")
46
+
47
+ # Extract text from image
48
+ ocr_model = load_ocr_model()
49
+ extracted_text = ocr_model(uploaded_file)["generated_text"]
50
+
51
+ if extracted_text:
52
+ st.write("### Extracted Text:")
53
+ st.write(extracted_text)
54
+
55
+ # Send text to ChatGPT
56
+ st.write("### ChatGPT Explanation:")
57
+ explanation = chat_with_gpt(extracted_text)
58
+ if explanation:
59
+ st.write(explanation)
60
+ else:
61
+ st.error("Could not extract text. Please try again with another image.")
62
+
63
+ if __name__ == "__main__":
64
+ main()