File size: 3,612 Bytes
abe8f23 69c0b63 abe8f23 69c0b63 abe8f23 69c0b63 abe8f23 69c0b63 abe8f23 69c0b63 abe8f23 8ee41bc abe8f23 69c0b63 abe8f23 69c0b63 abe8f23 69c0b63 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
import streamlit as st
import os
from llama_index.core import (
VectorStoreIndex,
SimpleDirectoryReader,
Settings,
)
from llama_index.core import PromptTemplate
from llama_index.llms.gemini import Gemini
from llama_index.embeddings.gemini import GeminiEmbedding
import logging
import google.generativeai as genai
from dotenv import load_dotenv
from pathlib import Path
load_dotenv()
# Set logging level
logging.basicConfig(level=logging.INFO)
# Configure Gemini Pro
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
model_gemini_pro_vision = "gemini-1.5-flash"
# Configure Gemini models
Settings.llm = Gemini(model=model_gemini_pro_vision,
api_key=os.getenv("GOOGLE_API_KEY"))
Settings.embed_model = GeminiEmbedding(
model_name="models/embedding-001",
api_key=os.getenv("GOOGLE_API_KEY")
)
def load_and_index_pdf(pdf_path):
"""Loads and index the pdf.
Args :
pdf_path (str) : The path to the pdf file
Returns :
index (llama_index.core.VectorStoreIndex): The vector index
"""
try:
logging.info(f"Loading PDF document from: {pdf_path}")
documents = SimpleDirectoryReader(input_files=[pdf_path]).load_data()
if documents:
logging.info("Creating vector store index")
index = VectorStoreIndex.from_documents(documents)
return index
else:
logging.warning("No documents found in the PDF")
return None
except Exception as e:
logging.error(f"Error loading and indexing PDF: {e}")
return None
def translate_text(french_text, index):
"""Translates french text to Yipunu.
Args :
french_text (str): The french text to translate.
index (llama_index.core.VectorStoreIndex): The vector index.
Returns:
(str): The yipunu translation or an error message.
"""
try:
logging.info(f"Initiating translation of: {french_text}")
template = (
"Tu es un excellent traducteur du français vers le yipunu. Tu traduis le texte sans donner d'explication. "
"Texte: {french_text} "
"Traduction:"
)
prompt_template = PromptTemplate(template)
query_engine = index.as_query_engine(
text_qa_template=prompt_template
)
response = query_engine.query(french_text)
logging.info(f"Translation Result: {response.response}")
return response.response
except Exception as e:
logging.error(f"Error during translation: {e}")
return f"Error during translation: {str(e)}"
def main():
"""Main function for streamlit app."""
st.title("French to Yipunu Translation App")
# PDF File Upload
uploaded_file = st.file_uploader("Upload a PDF file containing the Punu grammar:", type="pdf")
if uploaded_file is not None:
# Save file to a temporary location
temp_file_path = Path("temp_file.pdf")
with open(temp_file_path, "wb") as f:
f.write(uploaded_file.read())
index = load_and_index_pdf(str(temp_file_path))
if index:
french_text = st.text_area("Enter French Text:", "Ni vosi yipunu")
if st.button("Translate"):
translation = translate_text(french_text, index)
st.success(f"Yipunu Translation: {translation}")
# Clean up temp files
os.remove(temp_file_path)
else:
st.info("Please upload a pdf containing the punu grammar.")
if __name__ == "__main__":
main() |