Spaces:
Sleeping
Sleeping
AlihanAyverdi
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import google.generativeai as genai
|
3 |
+
import PyPDF2
|
4 |
+
import os
|
5 |
+
from dotenv import load_dotenv
|
6 |
+
|
7 |
+
# API anahtarınızı yükleyin
|
8 |
+
load_dotenv(override=True)
|
9 |
+
api_key = os.getenv("GEMINI_API_KEY")
|
10 |
+
genai.configure(api_key=api_key)
|
11 |
+
|
12 |
+
# Gemini modelini başlat
|
13 |
+
model = genai.GenerativeModel(model_name="gemini-1.5-flash",
|
14 |
+
system_instruction="Sen bir belge sohbet asistanısın. Sorulara sana verilen belgeler içindeki bilgileri tarayarak yanıt vermelisin. Aksi belirtilmediği sürece yanıtları Türkçe olarak ver."
|
15 |
+
)
|
16 |
+
|
17 |
+
def extract_text_from_pdf(pdf_files):
|
18 |
+
extracted_texts = []
|
19 |
+
for pdf_file in pdf_files:
|
20 |
+
with open(pdf_file.name, 'rb') as f:
|
21 |
+
pdf_reader = PyPDF2.PdfReader(f)
|
22 |
+
extracted_text = ""
|
23 |
+
for page in pdf_reader.pages:
|
24 |
+
text = page.extract_text()
|
25 |
+
if text:
|
26 |
+
extracted_text += text
|
27 |
+
extracted_texts.append(extracted_text)
|
28 |
+
return extracted_texts
|
29 |
+
|
30 |
+
def chat_with_documents(history, pdf_files):
|
31 |
+
if not history:
|
32 |
+
return history
|
33 |
+
|
34 |
+
extracted_texts = extract_text_from_pdf(pdf_files)
|
35 |
+
user_input = history[-1]['content']
|
36 |
+
prompt = [user_input] + extracted_texts
|
37 |
+
response = model.generate_content(prompt)
|
38 |
+
|
39 |
+
history.append({
|
40 |
+
'role': 'assistant',
|
41 |
+
'content': response.text
|
42 |
+
})
|
43 |
+
return history
|
44 |
+
|
45 |
+
with gr.Blocks() as demo:
|
46 |
+
gr.Markdown("# Belgelerinizle Sohbet Edin (Gemini API kullanarak)")
|
47 |
+
with gr.Accordion("Belgeler", open=False):
|
48 |
+
docs = gr.File(
|
49 |
+
label="Belgeleri Yükleyin",
|
50 |
+
file_count="multiple",
|
51 |
+
type="filepath"
|
52 |
+
)
|
53 |
+
chatbot = gr.Chatbot(type="messages")
|
54 |
+
msg = gr.Textbox(label="Mesajınız")
|
55 |
+
clear = gr.Button("Temizle")
|
56 |
+
|
57 |
+
def on_user_message(user_message, history):
|
58 |
+
history = history or []
|
59 |
+
history.append({
|
60 |
+
'role': 'user',
|
61 |
+
'content': user_message
|
62 |
+
})
|
63 |
+
return "", history
|
64 |
+
|
65 |
+
def on_generate_response(history, docs_value):
|
66 |
+
if not docs_value:
|
67 |
+
history.append({
|
68 |
+
'role': 'assistant',
|
69 |
+
'content': "Lütfen önce belge(leri) yükleyin."
|
70 |
+
})
|
71 |
+
return history
|
72 |
+
|
73 |
+
if history:
|
74 |
+
history = chat_with_documents(history, docs_value)
|
75 |
+
return history
|
76 |
+
|
77 |
+
msg.submit(on_user_message, [msg, chatbot], [msg, chatbot]).then(
|
78 |
+
on_generate_response, [chatbot, docs], chatbot
|
79 |
+
)
|
80 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
81 |
+
|
82 |
+
demo.launch()
|