shivanksharma commited on
Commit
9d38e7c
·
verified ·
1 Parent(s): a2d40f6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -11
app.py CHANGED
@@ -3,6 +3,7 @@ import requests
3
  import json
4
  from reportlab.lib.pagesizes import letter
5
  from reportlab.pdfgen import canvas
 
6
 
7
  # Hugging Face Inference API details
8
  HF_API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.3"
@@ -10,7 +11,6 @@ HF_API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-I
10
  import os
11
  HF_API_KEY = os.getenv("HF_API_KEY") # Read from environment variable
12
 
13
-
14
  HEADERS = {"Authorization": f"Bearer {HF_API_KEY}"}
15
 
16
  # Function to call Hugging Face API
@@ -38,20 +38,38 @@ def conduct_debate(topic):
38
 
39
  return response_1, response_2, conclusion
40
 
41
- # Function to generate a PDF
42
  def generate_pdf(topic, response_1, response_2, conclusion):
43
  pdf_filename = "debate_result.pdf"
44
-
45
  c = canvas.Canvas(pdf_filename, pagesize=letter)
46
- c.drawString(100, 750, f"Debate Topic: {topic}")
47
- c.drawString(100, 730, "Argument in Favor:")
48
- c.drawString(100, 710, response_1[:1000]) # Limiting text for PDF readability
49
- c.drawString(100, 690, "Argument Against:")
50
- c.drawString(100, 670, response_2[:1000])
51
- c.drawString(100, 650, "Judge's Conclusion:")
52
- c.drawString(100, 630, conclusion[:1000])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
  c.save()
54
-
55
  return pdf_filename
56
 
57
  # Gradio UI
 
3
  import json
4
  from reportlab.lib.pagesizes import letter
5
  from reportlab.pdfgen import canvas
6
+ from textwrap import wrap
7
 
8
  # Hugging Face Inference API details
9
  HF_API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.3"
 
11
  import os
12
  HF_API_KEY = os.getenv("HF_API_KEY") # Read from environment variable
13
 
 
14
  HEADERS = {"Authorization": f"Bearer {HF_API_KEY}"}
15
 
16
  # Function to call Hugging Face API
 
38
 
39
  return response_1, response_2, conclusion
40
 
41
+ # Function to generate a properly formatted PDF
42
  def generate_pdf(topic, response_1, response_2, conclusion):
43
  pdf_filename = "debate_result.pdf"
 
44
  c = canvas.Canvas(pdf_filename, pagesize=letter)
45
+ c.setFont("Helvetica", 12)
46
+ y_position = 750
47
+
48
+ def draw_wrapped_text(text, x, y, max_width=450):
49
+ lines = wrap(text, width=80) # Wrap text for better formatting
50
+ for line in lines:
51
+ c.drawString(x, y, line)
52
+ y -= 20
53
+ return y
54
+
55
+ c.drawString(100, y_position, f"Debate Topic: {topic}")
56
+ y_position -= 30
57
+
58
+ c.drawString(100, y_position, "Argument in Favor:")
59
+ y_position -= 20
60
+ y_position = draw_wrapped_text(response_1, 100, y_position)
61
+
62
+ y_position -= 30 # Space between sections
63
+ c.drawString(100, y_position, "Argument Against:")
64
+ y_position -= 20
65
+ y_position = draw_wrapped_text(response_2, 100, y_position)
66
+
67
+ y_position -= 30 # Space between sections
68
+ c.drawString(100, y_position, "Judge's Conclusion:")
69
+ y_position -= 20
70
+ y_position = draw_wrapped_text(conclusion, 100, y_position)
71
+
72
  c.save()
 
73
  return pdf_filename
74
 
75
  # Gradio UI