Spaces:
Sleeping
Sleeping
File size: 3,337 Bytes
83f6764 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
import os
import time
import streamlit as st
import together
import requests
# ✅ Load Together API key from environment variable
os.getenv("TOGETHERAI_API_KEY") == "9c679ff9d00b1c3e633ef3bb0ec44a8002a2b74d2f535d709b1bd85d26a168ee"
# ✅ Set API key for Together AI
together.api_key = "9c679ff9d00b1c3e633ef3bb0ec44a8002a2b74d2f535d709b1bd85d26a168ee"
# ✅ Function to load text from a URL
def load_text(url):
"""Fetches text from a URL"""
try:
headers = {"User-Agent": os.getenv("USER_AGENT", "Mozilla/5.0")}
response = requests.get(url, headers=headers)
response.raise_for_status()
return response.text[:20000] # Doubled input limit for more context
except Exception as e:
st.error(f"Error loading URL: {e}")
return None
# ✅ Function to summarize text with richer, more detailed output
def summarize_text(text, max_retries=3, retry_delay=5):
"""Summarize text using Together AI API with retry logic for extensive output"""
text = text[:20000] # Increased input limit to 20,000 characters (~5,000 tokens)
# Enhanced prompt for a more detailed, comprehensive summary
prompt = f"""
You are an expert summarizer tasked with creating a highly detailed, comprehensive, and well-structured summary of the url provided.
Provide a summary in 20-35 sentences that thoroughly captures the main points, key details, significant insights, and important examples or arguments presented in the text.
Ensure the summary is informative, coherent, and rich in content, avoiding vague or overly simplistic statements.
Include context where relevant and aim to give a complete picture of the text’s purpose and findings.
TEXT: {text}
SUMMARY:
"""
for attempt in range(max_retries):
try:
response = together.Complete.create(
model="mistralai/Mixtral-8x7B-Instruct-v0.1",
prompt=prompt,
max_tokens=600, # Increased to 600 tokens (~450-600 words) for a longer, detailed summary
temperature=0.3, # Kept low for focus and coherence
)
# Debugging: Print full API response (optional, remove in production)
print("Raw API Response:", response)
# Extract summary from response
if isinstance(response, dict) and "output" in response:
summary = response["output"]
return {"output_text": summary.strip()}
elif isinstance(response, dict) and "choices" in response:
summary = response["choices"][0]["text"]
return {"output_text": summary.strip()}
else:
raise KeyError("Unexpected API response format.")
except Exception as e:
if "rate_limit_exceeded" in str(e) and attempt < max_retries - 1:
st.warning(f"Attempt {attempt + 1}/{max_retries}: Rate limit exceeded. Retrying in {retry_delay} seconds...")
time.sleep(retry_delay)
else:
st.error(f"❌ Error during summarization: {e}")
return {"output_text": "Summarization failed. Try again later."}
return {"output_text": "Summarization failed after multiple attempts."} |