Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Import necessary libraries
|
2 |
+
import gradio as gr
|
3 |
+
import google.generativeai as genai
|
4 |
+
|
5 |
+
# Configure Google API
|
6 |
+
GOOGLE_API_KEY = 'AIzaSyAukxtFZ2tp0t9Xx-Fa0m3778xpoKEVuTY'
|
7 |
+
genai.configure(api_key=GOOGLE_API_KEY)
|
8 |
+
|
9 |
+
# Initialize the generative model
|
10 |
+
model = genai.GenerativeModel('gemini-1.5-flash')
|
11 |
+
|
12 |
+
# Upload the local .txt file
|
13 |
+
myfile = genai.upload_file(path="./Pride and Prejudice.txt", display_name="novel.txt", mime_type="text/plain")
|
14 |
+
|
15 |
+
# Function to handle user input and generate a response
|
16 |
+
def generate_response(user_question):
|
17 |
+
prompt = f"{user_question}"
|
18 |
+
|
19 |
+
# Generate content using the model
|
20 |
+
response = model.generate_content([prompt, myfile])
|
21 |
+
return response.text
|
22 |
+
|
23 |
+
# Create a Gradio interface
|
24 |
+
interface = gr.Interface(
|
25 |
+
fn=generate_response, # Function to call
|
26 |
+
inputs=gr.inputs.Textbox(label="Ask a question about Pride and Prejudice:"), # User input
|
27 |
+
outputs=gr.outputs.Textbox(label="Response from the novel:"), # Output display
|
28 |
+
title="Pride and Prejudice Q&A", # Title of the app
|
29 |
+
description="Ask any question about 'Pride and Prejudice' and get a detailed response!"
|
30 |
+
)
|
31 |
+
|
32 |
+
# Launch the app
|
33 |
+
if __name__ == "__main__":
|
34 |
+
interface.launch()
|