jehanzada commited on
Commit
f23f753
·
verified ·
1 Parent(s): 215bbce

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -19
app.py CHANGED
@@ -2,12 +2,11 @@ import os
2
  import gradio as gr
3
  from groq import Groq
4
  from dotenv import load_dotenv
5
- from datetime import datetime
6
  from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
7
  from reportlab.lib.styles import getSampleStyleSheet
8
  from reportlab.lib.units import inch
9
 
10
- # Load environment variables
11
  load_dotenv()
12
  GROQ_API_KEY = os.getenv("Freelancing_key")
13
 
@@ -17,10 +16,10 @@ client = Groq(api_key=GROQ_API_KEY)
17
  # AI RESPONSE FUNCTION
18
  # ===============================
19
 
20
- def generate_response(task, user_input, tone, chat_history):
21
 
22
- if chat_history is None:
23
- chat_history = []
24
 
25
  prompt = f"""
26
  Task: {task}
@@ -32,9 +31,10 @@ def generate_response(task, user_input, tone, chat_history):
32
  {"role": "system", "content": "You are a professional AI business assistant."}
33
  ]
34
 
35
- # Add previous messages properly
36
- for message in chat_history:
37
- messages.append(message)
 
38
 
39
  messages.append({"role": "user", "content": prompt})
40
 
@@ -46,43 +46,41 @@ def generate_response(task, user_input, tone, chat_history):
46
 
47
  answer = response.choices[0].message.content
48
 
49
- # Append properly formatted messages
50
- chat_history.append({"role": "user", "content": user_input})
51
- chat_history.append({"role": "assistant", "content": answer})
52
 
53
- return chat_history, chat_history
54
 
55
 
56
  # ===============================
57
  # PDF DOWNLOAD FUNCTION
58
  # ===============================
59
 
60
- def download_pdf(chat_history):
61
 
62
  file_path = "AI_Agent_Chat.pdf"
63
  doc = SimpleDocTemplate(file_path)
64
  elements = []
65
  styles = getSampleStyleSheet()
66
 
67
- for message in chat_history:
68
- role = message["role"]
69
- content = message["content"]
70
- elements.append(Paragraph(f"<b>{role.capitalize()}:</b> {content}", styles["Normal"]))
71
  elements.append(Spacer(1, 0.3 * inch))
 
 
72
 
73
  doc.build(elements)
74
  return file_path
75
 
76
 
77
  # ===============================
78
- # GRADIO UI
79
  # ===============================
80
 
81
  with gr.Blocks() as demo:
82
 
83
  gr.Markdown("# 💰 Freelancing AI Agent Pro")
84
 
85
- chatbot = gr.Chatbot(type="messages") # IMPORTANT FIX
86
  state = gr.State([])
87
 
88
  task = gr.Dropdown(
@@ -96,6 +94,7 @@ with gr.Blocks() as demo:
96
  )
97
 
98
  user_input = gr.Textbox(label="Enter Your Request")
 
99
  generate_btn = gr.Button("Generate")
100
  download_btn = gr.Button("Download as PDF")
101
 
 
2
  import gradio as gr
3
  from groq import Groq
4
  from dotenv import load_dotenv
 
5
  from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
6
  from reportlab.lib.styles import getSampleStyleSheet
7
  from reportlab.lib.units import inch
8
 
9
+ # Load API Key
10
  load_dotenv()
11
  GROQ_API_KEY = os.getenv("Freelancing_key")
12
 
 
16
  # AI RESPONSE FUNCTION
17
  # ===============================
18
 
19
+ def generate_response(task, user_input, tone, history):
20
 
21
+ if history is None:
22
+ history = []
23
 
24
  prompt = f"""
25
  Task: {task}
 
31
  {"role": "system", "content": "You are a professional AI business assistant."}
32
  ]
33
 
34
+ # Add previous chat correctly
35
+ for user_msg, bot_msg in history:
36
+ messages.append({"role": "user", "content": user_msg})
37
+ messages.append({"role": "assistant", "content": bot_msg})
38
 
39
  messages.append({"role": "user", "content": prompt})
40
 
 
46
 
47
  answer = response.choices[0].message.content
48
 
49
+ history.append((user_input, answer))
 
 
50
 
51
+ return history, history
52
 
53
 
54
  # ===============================
55
  # PDF DOWNLOAD FUNCTION
56
  # ===============================
57
 
58
+ def download_pdf(history):
59
 
60
  file_path = "AI_Agent_Chat.pdf"
61
  doc = SimpleDocTemplate(file_path)
62
  elements = []
63
  styles = getSampleStyleSheet()
64
 
65
+ for user_msg, bot_msg in history:
66
+ elements.append(Paragraph(f"<b>User:</b> {user_msg}", styles["Normal"]))
 
 
67
  elements.append(Spacer(1, 0.3 * inch))
68
+ elements.append(Paragraph(f"<b>AI:</b> {bot_msg}", styles["Normal"]))
69
+ elements.append(Spacer(1, 0.5 * inch))
70
 
71
  doc.build(elements)
72
  return file_path
73
 
74
 
75
  # ===============================
76
+ # GRADIO UI (Compatible Version)
77
  # ===============================
78
 
79
  with gr.Blocks() as demo:
80
 
81
  gr.Markdown("# 💰 Freelancing AI Agent Pro")
82
 
83
+ chatbot = gr.Chatbot() # NO type="messages"
84
  state = gr.State([])
85
 
86
  task = gr.Dropdown(
 
94
  )
95
 
96
  user_input = gr.Textbox(label="Enter Your Request")
97
+
98
  generate_btn = gr.Button("Generate")
99
  download_btn = gr.Button("Download as PDF")
100