Dua Rajper commited on
Commit
61621cb
·
verified ·
1 Parent(s): 1441a4f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -0
app.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import pipeline, AutoTokenizer, AutoModelForQuestionAnswering
3
+ from PIL import Image
4
+ import easyocr
5
+ import os
6
+ from groq import Groq
7
+
8
+ # OCR Function
9
+ def extract_text_from_image(image):
10
+ reader = easyocr.Reader(['en'])
11
+ result = reader.readtext(image)
12
+ extracted_text = " ".join([detection[1] for detection in result])
13
+ return extracted_text
14
+
15
+ # Question Answering Function (DistilBERT)
16
+ @st.cache_resource
17
+ def load_qa_model():
18
+ model_name = "distilbert/distilbert-base-cased-distilled-squad"
19
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
20
+ model = AutoModelForQuestionAnswering.from_pretrained(model_name)
21
+ nlp = pipeline('question-answering', model=model, tokenizer=tokenizer)
22
+ return nlp
23
+
24
+ def answer_question(context, question, qa_model):
25
+ result = qa_model({'question': question, 'context': context})
26
+ return result['answer']
27
+
28
+ # Groq API Function
29
+ def groq_chat(prompt):
30
+ try:
31
+ client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
32
+ chat_completion = client.chat.completions.create(
33
+ messages=[{"role": "user", "content": prompt}],
34
+ model="llama-3.3-70b-versatile",
35
+ )
36
+ return chat_completion.choices[0].message.content
37
+ except Exception as e:
38
+ return f"Error using Groq API: {e}. Please ensure GROQ_API_KEY is set correctly."
39
+
40
+ # Streamlit App
41
+ def main():
42
+ st.title("Image Text & Question Answering Chatbot")
43
+
44
+ uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
45
+
46
+ if uploaded_file is not None:
47
+ image = Image.open(uploaded_file)
48
+ st.image(image, caption="Uploaded Image", use_column_width=True)
49
+
50
+ if st.button("Extract Text and Enable Question Answering"):
51
+ with st.spinner("Extracting text..."):
52
+ extracted_text = extract_text_from_image(image)
53
+ st.write("Extracted Text:")
54
+ st.write(extracted_text)
55
+
56
+ qa_model = load_qa_model()
57
+
58
+ question = st.text_input("Ask a question about the image text:")
59
+ if st.button("Answer"):
60
+ if question:
61
+ with st.spinner("Answering..."):
62
+ answer = answer_question(extracted_text, question, qa_model)
63
+ st.write("Answer:", answer)
64
+ else:
65
+ st.warning("Please enter a question.")
66
+
67
+ # Groq Chat Section
68
+ st.subheader("General Chat (Powered by Groq)")
69
+ groq_prompt = st.text_input("Enter your message:")
70
+ if st.button("Send"):
71
+ if groq_prompt:
72
+ with st.spinner("Generating response..."):
73
+ groq_response = groq_chat(groq_prompt)
74
+ st.write("Response:", groq_response)
75
+ else:
76
+ st.warning("Please enter a message.")
77
+
78
+ if __name__ == "__main__":
79
+ main()