arpit13 commited on
Commit
d96d494
·
verified ·
1 Parent(s): c9b44ee

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -13
app.py CHANGED
@@ -2,9 +2,12 @@ import gradio as gr
2
  import openai
3
  import os
4
  import json
 
 
 
5
 
6
  # OpenAI API setup
7
- openai.api_key = os.getenv("GROQ_API_KEY")
8
  openai.api_base = "https://api.groq.com/openai/v1"
9
 
10
  # File to store conversation history
@@ -39,18 +42,29 @@ def clear_conversation_history():
39
  except Exception as e:
40
  return f"Error clearing history: {e}", ""
41
 
42
- # Function to format bot response
43
- def format_bot_response(response):
 
 
 
 
44
  """
45
- Converts markdown-like symbols to HTML and structures response.
 
 
46
  """
47
- response = response.replace("**", "<b>").replace("**", "</b>") # Bold formatting
48
- response = response.replace("1.", "<br>&nbsp;&nbsp;1.")
49
- response = response.replace("2.", "<br>&nbsp;&nbsp;2.")
50
- response = response.replace("3.", "<br>&nbsp;&nbsp;3.")
51
- response = response.replace("4.", "<br>&nbsp;&nbsp;4.")
52
- response = response.replace("5.", "<br>&nbsp;&nbsp;5.")
53
- return f"<div>{response}</div>"
 
 
 
 
 
54
 
55
  # Function to get response from the LLM
56
  def get_groq_response(message, history=[]):
@@ -60,10 +74,15 @@ def get_groq_response(message, history=[]):
60
  model="llama-3.1-70b-versatile",
61
  messages=messages
62
  )
63
- return format_bot_response(response.choices[0].message["content"])
64
  except Exception as e:
65
  return f"Error: {str(e)}"
66
 
 
 
 
 
 
67
  # Chatbot function
68
  def chatbot(user_input, history):
69
  # Load conversation history
@@ -76,6 +95,9 @@ def chatbot(user_input, history):
76
  # Get bot response
77
  bot_response = get_groq_response(user_input, formatted_history)
78
 
 
 
 
79
  # Update history with the new conversation
80
  conversation_history.append((user_input, bot_response))
81
 
@@ -91,7 +113,7 @@ def chatbot(user_input, history):
91
 
92
  return conversation_history, display_html, "" # Clear the user input field
93
 
94
- # Gradio Interface
95
  with gr.Blocks(css="""
96
  .user-message {
97
  background-color: #9ACBD0;
@@ -115,6 +137,21 @@ with gr.Blocks(css="""
115
  transform: scale(1.02);
116
  box-shadow: 0px 4px 10px rgba(0, 0, 0, 0.1);
117
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
118
  """) as demo:
119
  gr.Markdown("""# Mom: We have ChatGPT at Home, \n ChatGPT at Home: """)
120
 
@@ -134,3 +171,4 @@ with gr.Blocks(css="""
134
 
135
  # Launch the app
136
  demo.launch()
 
 
2
  import openai
3
  import os
4
  import json
5
+ import markdown
6
+ import re
7
+ import time
8
 
9
  # OpenAI API setup
10
+ openai.api_key = "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"
11
  openai.api_base = "https://api.groq.com/openai/v1"
12
 
13
  # File to store conversation history
 
42
  except Exception as e:
43
  return f"Error clearing history: {e}", ""
44
 
45
+ # Function to format code block
46
+ def format_code_block(code):
47
+ """Wraps the provided code in <pre> and <code> tags for proper display."""
48
+ return f"<pre><code>{code}</code></pre>"
49
+
50
+ def format_code_and_markdown(response):
51
  """
52
+ Handles both code blocks and markdown formatting simultaneously.
53
+ - Converts code blocks into HTML <pre><code> tags.
54
+ - Converts markdown syntax (e.g., bold, italics) into HTML tags.
55
  """
56
+ # Convert code blocks to HTML <pre><code>...</code></pre> format
57
+ def process_code_blocks(text):
58
+ code_block_pattern = re.compile(r'```python\n(.*?)```', re.DOTALL)
59
+ return re.sub(code_block_pattern, r'<pre><code>\1</code></pre>', text)
60
+
61
+ # Process the text to handle code blocks
62
+ response = process_code_blocks(response)
63
+
64
+ # Convert Markdown to HTML
65
+ html_response = markdown.markdown(response)
66
+
67
+ return html_response
68
 
69
  # Function to get response from the LLM
70
  def get_groq_response(message, history=[]):
 
74
  model="llama-3.1-70b-versatile",
75
  messages=messages
76
  )
77
+ return format_code_and_markdown(response.choices[0].message["content"])
78
  except Exception as e:
79
  return f"Error: {str(e)}"
80
 
81
+ # Function to simulate typing effect
82
+ def simulate_typing_effect(response, delay=1):
83
+ time.sleep(delay)
84
+ return response
85
+
86
  # Chatbot function
87
  def chatbot(user_input, history):
88
  # Load conversation history
 
95
  # Get bot response
96
  bot_response = get_groq_response(user_input, formatted_history)
97
 
98
+ # Simulate typing delay
99
+ bot_response = simulate_typing_effect(bot_response)
100
+
101
  # Update history with the new conversation
102
  conversation_history.append((user_input, bot_response))
103
 
 
113
 
114
  return conversation_history, display_html, "" # Clear the user input field
115
 
116
+ # Gradio Interface with enhanced UI/UX styling
117
  with gr.Blocks(css="""
118
  .user-message {
119
  background-color: #9ACBD0;
 
137
  transform: scale(1.02);
138
  box-shadow: 0px 4px 10px rgba(0, 0, 0, 0.1);
139
  }
140
+ .chat-container {
141
+ max-height: 500px;
142
+ overflow-y: auto;
143
+ margin-bottom: 20px;
144
+ }
145
+ .gradio-button {
146
+ background-color: #4CAF50;
147
+ color: white;
148
+ border-radius: 5px;
149
+ padding: 10px 20px;
150
+ font-size: 16px;
151
+ }
152
+ .gradio-button:hover {
153
+ background-color: #45a049;
154
+ }
155
  """) as demo:
156
  gr.Markdown("""# Mom: We have ChatGPT at Home, \n ChatGPT at Home: """)
157
 
 
171
 
172
  # Launch the app
173
  demo.launch()
174
+