Spaces:
Sleeping
Sleeping
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."} |