tensorboy0101 commited on
Commit
545cc36
·
verified ·
1 Parent(s): 5d27a9d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -52
app.py CHANGED
@@ -1,52 +1,12 @@
1
- import streamlit as st
2
- from transformers import AutoTokenizer, AutoModelForCausalLM
3
- import PyPDF2
4
-
5
- # Model Identifier
6
- model_name = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
7
- tokenizer = AutoTokenizer.from_pretrained(model_name)
8
- model = AutoModelForCausalLM.from_pretrained(model_name)
9
-
10
- st.set_page_config(page_title="DOCAI with TinyLlama", page_icon="🤖", layout="wide")
11
- st.title("📄 Document Chat with TinyLlama 🤖")
12
-
13
- # Initialize chat history
14
- if "messages" not in st.session_state:
15
- st.session_state["messages"] = [{"role": "assistant", "content": "How can I help you with your document?"}]
16
-
17
- def process_document_and_respond(uploaded_file, question):
18
- if uploaded_file:
19
- try:
20
- reader = PyPDF2.PdfReader(uploaded_file)
21
- text_content = ""
22
- for page in reader.pages:
23
- text_content += page.extract_text()
24
-
25
- # Basic context injection - you might need a more sophisticated approach
26
- prompt = f"Context: {text_content}\n\nQuestion: {question}\n\nAnswer:"
27
- inputs = tokenizer([prompt], return_tensors="pt").to(model.device)
28
- outputs = model.generate(**inputs, max_new_tokens=512, do_sample=True, top_p=0.9, top_k=50, temperature=0.7)
29
- response = tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]
30
- return response.split("Answer:")[-1].strip()
31
-
32
- except Exception as e:
33
- return f"Error processing document: {e}"
34
- else:
35
- return "Please upload a PDF document first."
36
-
37
- # Upload the PDF
38
- uploaded_file = st.file_uploader("Upload a PDF document", type="pdf")
39
-
40
- # Input for the question
41
- question = st.text_input("Ask your question about the document:")
42
-
43
- if question:
44
- with st.spinner("Thinking..."):
45
- response = process_document_and_respond(uploaded_file, question)
46
- st.session_state["messages"].append({"role": "user", "content": question})
47
- st.session_state["messages"].append({"role": "assistant", "content": response})
48
-
49
- # Display chat history
50
- for message in st.session_state["messages"]:
51
- with st.chat_message(message["role"]):
52
- st.markdown(message["content"])
 
1
+ import os
2
+ from langchain_google_genai import ChatGoogleGenerativeAI
3
+ from dotenv import load_dotenv
4
+
5
+ load_dotenv()
6
+ llm = ChatGoogleGenerativeAI(
7
+ model="models/gemini-pro",
8
+ temperature=0.7
9
+ )
10
+
11
+ response = llm.invoke("Hello, what is AI?")
12
+ print(response.content)