Alibrown commited on
Commit
047f1a7
·
verified ·
1 Parent(s): 3757721

Create new_app.py

Browse files
Files changed (1) hide show
  1. new_app.py +73 -0
new_app.py ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import google.generativeai as genai
3
+ from PIL import Image
4
+ import io
5
+ import base64
6
+ import pandas as pd
7
+ import zipfile
8
+ import os
9
+ import PyPDF2
10
+
11
+ def encode_image(image):
12
+ """Convert PIL Image to base64 string"""
13
+ buffered = io.BytesIO()
14
+ image.save(buffered, format="JPEG")
15
+ image_bytes = buffered.getvalue()
16
+ encoded_image = base64.b64encode(image_bytes).decode('utf-8')
17
+ return encoded_image
18
+
19
+ def read_file(uploaded_file):
20
+ """Reads different file formats and returns content as string"""
21
+ file_type = uploaded_file.name.split('.')[-1].lower()
22
+
23
+ if file_type in ["txt", "html", "css", "php", "js", "py", "java", "c", "cpp"]:
24
+ return uploaded_file.read().decode("utf-8")
25
+
26
+ elif file_type in ["csv", "xlsx"]:
27
+ df = pd.read_csv(uploaded_file) if file_type == "csv" else pd.read_excel(uploaded_file)
28
+ return df.to_string()
29
+
30
+ elif file_type == "pdf":
31
+ reader = PyPDF2.PdfReader(uploaded_file)
32
+ text = "".join([page.extract_text() for page in reader.pages if page.extract_text()])
33
+ return text
34
+
35
+ elif file_type == "zip":
36
+ with zipfile.ZipFile(uploaded_file, 'r') as zip_ref:
37
+ file_list = zip_ref.namelist()
38
+ extracted_text = "\n".join(file_list)
39
+ return f"ZIP Inhalt: {extracted_text}"
40
+
41
+ return "Nicht unterstütztes Dateiformat."
42
+
43
+ st.set_page_config(page_title="Gemini AI Chat", layout="wide")
44
+ st.title("🤖 Gemini AI Chat Interface")
45
+
46
+ api_key = st.text_input("Enter Google AI API Key", type="password")
47
+ model = st.selectbox("Select Model", [
48
+ "gemini-1.5-flash", "gemini-1.5-pro", "gemini-1.5-flash-8B", "gemini-1.5-pro-vision-latest",
49
+ "gemini-1.0-pro", "gemini-1.0-pro-vision-latest", "gemini-2.0-pro-exp-02-05", "gemini-2.0-flash-lite",
50
+ "gemini-2.0-flash-exp-image-generation", "gemini-2.0-flash", "gemini-2.0-flash-thinking-exp-01-21"
51
+ ])
52
+
53
+ uploaded_file = st.file_uploader("Upload a file (optional)", type=["txt", "csv", "xlsx", "pdf", "zip", "html", "css", "php", "js", "py", "java", "c", "cpp"])
54
+ file_content = None
55
+ if uploaded_file:
56
+ file_content = read_file(uploaded_file)
57
+ st.text_area("File Content Preview", file_content, height=200)
58
+
59
+ user_input = st.text_area("Your message")
60
+
61
+ if st.button("Send") and api_key:
62
+ try:
63
+ genai.configure(api_key=api_key)
64
+ model_instance = genai.GenerativeModel(model_name=model)
65
+
66
+ content = [{"text": user_input}]
67
+ if file_content:
68
+ content.append({"text": file_content})
69
+
70
+ response = model_instance.generate_content(content)
71
+ st.markdown(response.text)
72
+ except Exception as e:
73
+ st.error(f"Error: {str(e)}")