Kvikontent commited on
Commit
be69a8e
1 Parent(s): 67c050f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -46
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
- """Converts text to Markdown format with proper indentation.
11
-
12
- Args:
13
- text (str): The text to convert.
14
-
15
- Returns:
16
- str: The converted Markdown text.
17
- """
18
-
19
- text = text.replace('•', ' *')
20
- return Markdown(textwrap.indent(text, '> ', predicate=lambda _: True))
21
-
22
-
23
- def chat(user_message):
24
- """Generates a response to the user's message.
25
-
26
- Args:
27
- user_message (str): The user's message.
28
-
29
- Returns:
30
- str: The AI-generated response (or a message indicating unavailability).
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
- interface.launch()
 
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()