Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -2,53 +2,30 @@ import gradio as gr
|
|
2 |
import pathlib
|
3 |
import textwrap
|
4 |
import google.generativeai as genai
|
5 |
-
from IPython.display import display # Only for development/testing
|
6 |
-
from IPython.display import Markdown # Only for development/testing
|
7 |
-
|
8 |
|
9 |
def to_markdown(text):
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
"
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
genai.configure(api_key='AIzaSyCMBk81YmILNTok8hd6tYtJaevp1qbl6I0') # Replace with your actual API key
|
34 |
-
model = genai.GenerativeModel('gemini-pro')
|
35 |
-
|
36 |
-
try:
|
37 |
-
response = model.generate_content(user_message, stream=True)
|
38 |
-
for chunk in response:
|
39 |
-
return chunk.text # Return the first generated text chunk
|
40 |
-
except Exception as e:
|
41 |
-
print(f"Error during generation: {e}")
|
42 |
-
return "An error occurred while generating the response. Please try again later."
|
43 |
-
|
44 |
-
|
45 |
-
interface = gr.Interface(
|
46 |
-
fn=chat,
|
47 |
-
inputs="textbox",
|
48 |
-
outputs="textbox",
|
49 |
-
title="Gemini App",
|
50 |
-
description="Chat with an AI assistant",
|
51 |
-
theme="soft"
|
52 |
)
|
53 |
|
54 |
-
|
|
|
2 |
import pathlib
|
3 |
import textwrap
|
4 |
import google.generativeai as genai
|
|
|
|
|
|
|
5 |
|
6 |
def to_markdown(text):
|
7 |
+
"""Converts text to Markdown format with proper indentation."""
|
8 |
+
text = text.replace('•', ' *')
|
9 |
+
return textwrap.indent(text, '> ', lambda line: True)
|
10 |
+
|
11 |
+
def chat(message, history):
|
12 |
+
"""Generates a response to the user's message using the Gemini API."""
|
13 |
+
genai.configure(api_key='AIzaSyCMBk81YmILNTok8hd6tYtJaevp1qbl6I0') # Replace with your actual API key
|
14 |
+
model = genai.GenerativeModel('gemini-pro')
|
15 |
+
|
16 |
+
try:
|
17 |
+
response = model.generate_content(message, stream=True)
|
18 |
+
for chunk in response:
|
19 |
+
return to_markdown(chunk.text) # Format as Markdown
|
20 |
+
except Exception as e:
|
21 |
+
print(f"Error during generation: {e}")
|
22 |
+
return "An error occurred while generating the response. Please try again later."
|
23 |
+
|
24 |
+
chat_interface = gr.ChatInterface(
|
25 |
+
fn=chat,
|
26 |
+
title="Gemini Chat",
|
27 |
+
description="Chat with an AI assistant powered by Gemini",
|
28 |
+
theme="soft"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
)
|
30 |
|
31 |
+
chat_interface.launch()
|