JSenkCC commited on
Commit
fab4124
·
verified ·
1 Parent(s): 414b2d2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -14
app.py CHANGED
@@ -390,11 +390,31 @@ def clean_output(output):
390
  def validate_and_generate_documentation(api_url, headers, gemini_output, functionality_description):
391
  """
392
  Uses the Hugging Face Inference API to generate clean and relevant documentation using Qwen.
 
393
  """
394
- # Clean Gemini output
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
395
  cleaned_gemini_output = extract_cleaned_gemini_output(gemini_output)
 
396
 
397
- # Generate the refined prompt for Qwen
398
  prompt = f"""
399
  User-specified functionality: '{functionality_description}'
400
  Functions identified by Gemini:
@@ -433,6 +453,8 @@ def validate_and_generate_documentation(api_url, headers, gemini_output, functio
433
  5. Return only the required information for the above tasks, and exclude everything else.
434
  """
435
 
 
 
436
  payload = {"inputs": prompt, "parameters": {"max_new_tokens": 1024}}
437
  response = requests.post(api_url, headers=headers, json=payload)
438
 
@@ -441,23 +463,49 @@ def validate_and_generate_documentation(api_url, headers, gemini_output, functio
441
  api_response = response.json()
442
  output = api_response.get("generated_text", "") if isinstance(api_response, dict) else api_response[0].get("generated_text", "")
443
 
444
- # Extract only the relevant sections by identifying key markers in the output
445
- relevant_sections = []
446
- capturing = False
447
-
448
- for line in output.splitlines():
449
- if "Project Summary:" in line or "Functionality Summary:" in line or "Functionality Flow:" in line or "Function Documentation:" in line:
450
- capturing = True # Start capturing from relevant sections
451
- if capturing:
452
- relevant_sections.append(line)
453
-
454
- # Join relevant sections and perform final cleanup
455
- return clean_output("\n".join(relevant_sections))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
456
  else:
457
  raise ValueError(f"Error during API call: {response.status_code}, {response.text}")
458
 
459
 
460
 
 
461
  def generate_documentation_page():
462
  st.subheader(f"Generate Documentation for {st.session_state.current_project}")
463
  st.write("Enter the functionality or parts of the project for which you'd like to generate documentation.")
 
390
  def validate_and_generate_documentation(api_url, headers, gemini_output, functionality_description):
391
  """
392
  Uses the Hugging Face Inference API to generate clean and relevant documentation using Qwen.
393
+ Handles truncated output by identifying missing functions and re-prompting Qwen.
394
  """
395
+ def get_missing_functions(output, cleaned_gemini_output):
396
+ """
397
+ Identifies functions mentioned in Gemini's output that are missing in Qwen's response.
398
+ """
399
+ gemini_functions = set()
400
+ for line in cleaned_gemini_output.splitlines():
401
+ if line.startswith("-public "): # Assuming function definitions start with "-public"
402
+ function_signature = line.split(":")[0]
403
+ gemini_functions.add(function_signature.strip())
404
+
405
+ qwen_functions = set()
406
+ for line in output.splitlines():
407
+ if line.startswith("- **Function**:"): # Qwen's format for functions
408
+ function_signature = line.replace("- **Function**:", "").strip().split("`")[1]
409
+ qwen_functions.add(function_signature.strip())
410
+
411
+ # Find functions in Gemini's output but missing in Qwen's response
412
+ return gemini_functions - qwen_functions
413
+
414
  cleaned_gemini_output = extract_cleaned_gemini_output(gemini_output)
415
+ gemini_output_length = len(cleaned_gemini_output) # Record the length of the cleaned Gemini output
416
 
417
+ # Generate the initial refined prompt for Qwen
418
  prompt = f"""
419
  User-specified functionality: '{functionality_description}'
420
  Functions identified by Gemini:
 
453
  5. Return only the required information for the above tasks, and exclude everything else.
454
  """
455
 
456
+ qwen_prompt_length = len(prompt)
457
+
458
  payload = {"inputs": prompt, "parameters": {"max_new_tokens": 1024}}
459
  response = requests.post(api_url, headers=headers, json=payload)
460
 
 
463
  api_response = response.json()
464
  output = api_response.get("generated_text", "") if isinstance(api_response, dict) else api_response[0].get("generated_text", "")
465
 
466
+ # Remove the Gemini content from the top of the Qwen output
467
+ trimmed_output = output[gemini_output_length + qwen_prompt_length:].strip()
468
+
469
+ # Check for missing functions
470
+ missing_functions = get_missing_functions(trimmed_output, cleaned_gemini_output)
471
+
472
+ if missing_functions:
473
+ # Re-prompt Qwen for the missing functions
474
+ missing_functions_prompt = f"""
475
+ User-specified functionality: '{functionality_description}'
476
+ The following functions were not fully documented in the previous response:
477
+ {', '.join(missing_functions)}
478
+
479
+ Please provide detailed documentation for these functions in the following format:
480
+ '
481
+ Function Documentation:
482
+ For each function:
483
+ - Summary: <Description of the function's purpose>
484
+ - Inputs: <Details of inputs and their types>
485
+ - Outputs: <Details of outputs and their types>
486
+ - Dependencies: <Dependencies on other modules/functions>
487
+ - Data structures: <Details of data structures used>
488
+ - Algorithmic Details: <Description of the algorithm used>
489
+ - Error Handling: <Description of how the function handles errors>
490
+ - Assumptions: <Any assumptions the function makes>
491
+ - Example Usage: <Example demonstrating usage>
492
+ '
493
+ """
494
+ missing_payload = {"inputs": missing_functions_prompt, "parameters": {"max_new_tokens": 1024}}
495
+ missing_response = requests.post(api_url, headers=headers, json=missing_payload)
496
+
497
+ if missing_response.status_code == 200:
498
+ missing_api_response = missing_response.json()
499
+ missing_output = missing_api_response.get("generated_text", "") if isinstance(missing_api_response, dict) else missing_api_response[0].get("generated_text", "")
500
+ trimmed_output += "\n\n" + missing_output
501
+
502
+ return clean_output(trimmed_output) # Final cleanup if necessary
503
  else:
504
  raise ValueError(f"Error during API call: {response.status_code}, {response.text}")
505
 
506
 
507
 
508
+
509
  def generate_documentation_page():
510
  st.subheader(f"Generate Documentation for {st.session_state.current_project}")
511
  st.write("Enter the functionality or parts of the project for which you'd like to generate documentation.")