File size: 2,709 Bytes
047f1a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import google.generativeai as genai
from PIL import Image
import io
import base64
import pandas as pd
import zipfile
import os
import PyPDF2

def encode_image(image):
    """Convert PIL Image to base64 string"""
    buffered = io.BytesIO()
    image.save(buffered, format="JPEG")
    image_bytes = buffered.getvalue()
    encoded_image = base64.b64encode(image_bytes).decode('utf-8')
    return encoded_image

def read_file(uploaded_file):
    """Reads different file formats and returns content as string"""
    file_type = uploaded_file.name.split('.')[-1].lower()
    
    if file_type in ["txt", "html", "css", "php", "js", "py", "java", "c", "cpp"]:
        return uploaded_file.read().decode("utf-8")
    
    elif file_type in ["csv", "xlsx"]:
        df = pd.read_csv(uploaded_file) if file_type == "csv" else pd.read_excel(uploaded_file)
        return df.to_string()
    
    elif file_type == "pdf":
        reader = PyPDF2.PdfReader(uploaded_file)
        text = "".join([page.extract_text() for page in reader.pages if page.extract_text()])
        return text
    
    elif file_type == "zip":
        with zipfile.ZipFile(uploaded_file, 'r') as zip_ref:
            file_list = zip_ref.namelist()
            extracted_text = "\n".join(file_list)
            return f"ZIP Inhalt: {extracted_text}"
    
    return "Nicht unterstütztes Dateiformat."

st.set_page_config(page_title="Gemini AI Chat", layout="wide")
st.title("🤖 Gemini AI Chat Interface")

api_key = st.text_input("Enter Google AI API Key", type="password")
model = st.selectbox("Select Model", [
    "gemini-1.5-flash", "gemini-1.5-pro", "gemini-1.5-flash-8B", "gemini-1.5-pro-vision-latest", 
    "gemini-1.0-pro", "gemini-1.0-pro-vision-latest", "gemini-2.0-pro-exp-02-05", "gemini-2.0-flash-lite", 
    "gemini-2.0-flash-exp-image-generation", "gemini-2.0-flash", "gemini-2.0-flash-thinking-exp-01-21"
])

uploaded_file = st.file_uploader("Upload a file (optional)", type=["txt", "csv", "xlsx", "pdf", "zip", "html", "css", "php", "js", "py", "java", "c", "cpp"])
file_content = None
if uploaded_file:
    file_content = read_file(uploaded_file)
    st.text_area("File Content Preview", file_content, height=200)

user_input = st.text_area("Your message")

if st.button("Send") and api_key:
    try:
        genai.configure(api_key=api_key)
        model_instance = genai.GenerativeModel(model_name=model)
        
        content = [{"text": user_input}]
        if file_content:
            content.append({"text": file_content})
        
        response = model_instance.generate_content(content)
        st.markdown(response.text)
    except Exception as e:
        st.error(f"Error: {str(e)}")